test/jdk/java/net/httpclient/security/Security.java
author chegar
Thu, 07 Dec 2017 11:54:55 +0000
branchhttp-client-branch
changeset 55973 4d9b002587db
parent 55819 18e431209168
parent 48083 b1c1b4ef4be2
child 56089 42208b2f224e
permissions -rw-r--r--
http-client-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     1
/*
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     4
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
     7
 * published by the Free Software Foundation.
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     8
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    13
 * accompanied this code).
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    14
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    18
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
40684
2e37c119dc2a 8164982: Fix legal notices in java/lang, java/net, java/util tests.
shurailine
parents: 38883
diff changeset
    21
 * questions.
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    22
 */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    23
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    24
/*
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    25
 * @test
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    26
 * @bug 8087112
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    27
 * @modules jdk.incubator.httpclient
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    28
 *          java.logging
38883
d5de564f8089 8151913: Fix module dependencies in java/net tests
mli
parents: 37897
diff changeset
    29
 *          jdk.httpserver
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    30
 * @library /lib/testlibrary/
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    31
 * @build jdk.testlibrary.SimpleSSLContext
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    32
 * @compile ../../../../com/sun/net/httpserver/LogFilter.java
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    33
 * @compile ../../../../com/sun/net/httpserver/FileServerHandler.java
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    34
 * @compile ../ProxyServer.java
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    35
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    36
 * @run main/othervm/secure=java.lang.SecurityManager/policy=0.policy Security 0
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    37
 * @run main/othervm/secure=java.lang.SecurityManager/policy=2.policy Security 2
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    38
 * @run main/othervm/secure=java.lang.SecurityManager/policy=3.policy Security 3
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    39
 * @run main/othervm/secure=java.lang.SecurityManager/policy=4.policy Security 4
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    40
 * @run main/othervm/secure=java.lang.SecurityManager/policy=5.policy Security 5
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    41
 * @run main/othervm/secure=java.lang.SecurityManager/policy=6.policy Security 6
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    42
 * @run main/othervm/secure=java.lang.SecurityManager/policy=7.policy Security 7
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    43
 * @run main/othervm/secure=java.lang.SecurityManager/policy=8.policy Security 8
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    44
 * @run main/othervm/secure=java.lang.SecurityManager/policy=9.policy Security 9
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    45
 * @run main/othervm/secure=java.lang.SecurityManager/policy=0.policy Security 13
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
    46
 * @run main/othervm/secure=java.lang.SecurityManager/policy=14.policy Security 14
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    47
 * @run main/othervm/secure=java.lang.SecurityManager/policy=15.policy -Djava.security.debug=access:domain,failure Security 15
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    48
 */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    49
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
    50
// Tests 1, 10, 11 and 12 executed from Driver
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
    51
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    52
import com.sun.net.httpserver.Headers;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    53
import com.sun.net.httpserver.HttpContext;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    54
import com.sun.net.httpserver.HttpExchange;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    55
import com.sun.net.httpserver.HttpHandler;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    56
import com.sun.net.httpserver.HttpServer;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    57
import com.sun.net.httpserver.HttpsServer;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    58
import java.io.IOException;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    59
import java.io.InputStream;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    60
import java.io.File;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    61
import java.io.OutputStream;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    62
import java.lang.reflect.Constructor;
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    63
import java.net.BindException;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    64
import java.net.InetSocketAddress;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    65
import java.net.ProxySelector;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    66
import java.net.URI;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    67
import java.net.URLClassLoader;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    68
import java.net.URL;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    69
import jdk.incubator.http.HttpHeaders;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    70
import jdk.incubator.http.HttpClient;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    71
import jdk.incubator.http.HttpRequest;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    72
import jdk.incubator.http.HttpResponse;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    73
import java.nio.charset.StandardCharsets;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    74
import java.nio.file.Files;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    75
import java.nio.file.Path;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    76
import java.nio.ByteBuffer;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    77
import java.nio.file.Paths;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    78
import java.nio.file.StandardCopyOption;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    79
import java.util.LinkedList;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    80
import java.util.List;
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    81
import java.util.concurrent.CompletableFuture;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    82
import java.util.concurrent.CompletionException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    83
import java.util.concurrent.CompletionStage;
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    84
import java.util.concurrent.ExecutionException;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    85
import java.util.concurrent.Executors;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    86
import java.util.concurrent.ExecutorService;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    87
import java.util.concurrent.Flow;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    88
import java.util.logging.ConsoleHandler;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    89
import java.util.logging.Level;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    90
import java.util.logging.Logger;
37897
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
    91
import java.lang.reflect.InvocationTargetException;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    92
import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    93
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    94
/**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    95
 * Security checks test
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    96
 */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    97
public class Security {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    98
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    99
    static HttpServer s1 = null;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   100
    static ExecutorService executor=null;
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   101
    static int port, proxyPort;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   102
    static HttpClient client;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   103
    static String httproot, fileuri, fileroot, redirectroot;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   104
    static List<HttpClient> clients = new LinkedList<>();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   105
    static URI uri;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   106
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   107
    interface Test {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   108
        void execute() throws IOException, InterruptedException;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   109
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   110
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   111
    static class TestAndResult {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   112
        Test test;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   113
        boolean result;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   114
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   115
        TestAndResult (Test t, boolean result) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   116
            this.test = t;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   117
            this.result = result;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   118
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   119
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   120
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   121
    static TestAndResult test(boolean result, Test t) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   122
        return new TestAndResult(t, result);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   123
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   124
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   125
    static TestAndResult[] tests;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   126
    static String testclasses;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   127
    static File subdir;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   128
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   129
    /**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   130
     * The ProxyServer class is compiled by jtreg, but we want to
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   131
     * move it so it is not on the application claspath. We want to
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   132
     * load it through a separate classloader so that it has a separate
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   133
     * protection domain and security permissions.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   134
     *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   135
     * Its permissions are in the second grant block in each policy file
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   136
     */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   137
    static void setupProxy() throws IOException, ClassNotFoundException, NoSuchMethodException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   138
        testclasses = System.getProperty("test.classes");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   139
        subdir = new File (testclasses, "proxydir");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   140
        subdir.mkdir();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   141
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   142
        movefile("ProxyServer.class");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   143
        movefile("ProxyServer$Connection.class");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   144
        movefile("ProxyServer$1.class");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   145
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   146
        URL url = subdir.toURL();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   147
        System.out.println("URL for class loader = " + url);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   148
        URLClassLoader urlc = new URLClassLoader(new URL[] {url});
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   149
        proxyClass = Class.forName("ProxyServer", true, urlc);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   150
        proxyConstructor = proxyClass.getConstructor(Integer.class, Boolean.class);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   151
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   152
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   153
    static void movefile(String f) throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   154
        Path src = Paths.get(testclasses, f);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   155
        Path dest = subdir.toPath().resolve(f);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   156
        if (!dest.toFile().exists()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   157
            System.out.printf("moving %s to %s\n", src.toString(), dest.toString());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   158
            Files.move(src, dest,  StandardCopyOption.REPLACE_EXISTING);
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   159
        } else if (src.toFile().exists()) {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   160
            System.out.printf("%s exists, deleting %s\n", dest.toString(), src.toString());
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   161
            Files.delete(src);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   162
        } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   163
            System.out.printf("NOT moving %s to %s\n", src.toString(), dest.toString());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   164
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   165
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   166
37897
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   167
    static Object getProxy(int port, boolean b) throws Throwable {
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   168
        try {
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   169
            return proxyConstructor.newInstance(port, b);
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   170
        } catch (InvocationTargetException e) {
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   171
            throw e.getTargetException();
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   172
        }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   173
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   174
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   175
    static Class<?> proxyClass;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   176
    static Constructor<?> proxyConstructor;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   177
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   178
    static void setupTests() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   179
        tests = new TestAndResult[]{
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   180
            // (0) policy does not have permission for file. Should fail
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   181
            test(false, () -> { // Policy 0
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   182
                URI u = URI.create("http://127.0.0.1:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   183
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   184
                HttpResponse<?> response = client.send(request, asString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   185
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   186
            // (1) policy has permission for file URL
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   187
            test(true, () -> { //Policy 1
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   188
                URI u = URI.create("http://127.0.0.1:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   189
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   190
                HttpResponse<?> response = client.send(request, asString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   191
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   192
            // (2) policy has permission for all file URLs under /files
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   193
            test(true, () -> { // Policy 2
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   194
                URI u = URI.create("http://127.0.0.1:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   195
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   196
                HttpResponse<?> response = client.send(request, asString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   197
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   198
            // (3) policy has permission for first URL but not redirected URL
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   199
            test(false, () -> { // Policy 3
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   200
                URI u = URI.create("http://127.0.0.1:" + port + "/redirect/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   201
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   202
                HttpResponse<?> response = client.send(request, asString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   203
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   204
            // (4) policy has permission for both first URL and redirected URL
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   205
            test(true, () -> { // Policy 4
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   206
                URI u = URI.create("http://127.0.0.1:" + port + "/redirect/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   207
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   208
                HttpResponse<?> response = client.send(request, asString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   209
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   210
            // (5) policy has permission for redirected but not first URL
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   211
            test(false, () -> { // Policy 5
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   212
                URI u = URI.create("http://127.0.0.1:" + port + "/redirect/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   213
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   214
                HttpResponse<?> response = client.send(request, asString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   215
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   216
            // (6) policy has permission for file URL, but not method
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   217
            test(false, () -> { //Policy 6
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   218
                URI u = URI.create("http://127.0.0.1:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   219
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   220
                HttpResponse<?> response = client.send(request, asString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   221
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   222
            // (7) policy has permission for file URL, method, but not header
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   223
            test(false, () -> { //Policy 7
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   224
                URI u = URI.create("http://127.0.0.1:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   225
                HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   226
                                                 .header("X-Foo", "bar")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   227
                                                 .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   228
                                                 .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   229
                HttpResponse<?> response = client.send(request, asString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   230
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   231
            // (8) policy has permission for file URL, method and header
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   232
            test(true, () -> { //Policy 8
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   233
                URI u = URI.create("http://127.0.0.1:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   234
                HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   235
                                                 .header("X-Foo", "bar")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   236
                                                 .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   237
                                                 .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   238
                HttpResponse<?> response = client.send(request, asString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   239
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   240
            // (9) policy has permission for file URL, method and header
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   241
            test(true, () -> { //Policy 9
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   242
                URI u = URI.create("http://127.0.0.1:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   243
                HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   244
                                                 .headers("X-Foo", "bar", "X-Bar", "foo")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   245
                                                 .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   246
                                                 .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   247
                HttpResponse<?> response = client.send(request, asString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   248
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   249
            // (10) policy has permission for destination URL but not for proxy
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   250
            test(false, () -> { //Policy 10
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   251
                directProxyTest(proxyPort, true);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   252
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   253
            // (11) policy has permission for both destination URL and proxy
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   254
            test(true, () -> { //Policy 11
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   255
                directProxyTest(proxyPort, true);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   256
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   257
            // (12) policy has permission for both destination URL and proxy
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   258
            test(false, () -> { //Policy 11
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   259
                directProxyTest(proxyPort, false);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   260
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   261
            // (13) async version of test 0
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   262
            test(false, () -> { // Policy 0
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   263
                URI u = URI.create("http://127.0.0.1:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   264
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   265
                try {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   266
                    HttpResponse<?> response = client.sendAsync(request, asString()).get();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   267
                } catch (ExecutionException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   268
                    if (e.getCause() instanceof SecurityException) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   269
                        throw (SecurityException)e.getCause();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   270
                    } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   271
                        throw new RuntimeException(e);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   272
                    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   273
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   274
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   275
            // (14) async version of test 1
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   276
            test(true, () -> { //Policy 1
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   277
                URI u = URI.create("http://127.0.0.1:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   278
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   279
                try {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   280
                    HttpResponse<?> response = client.sendAsync(request, asString()).get();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   281
                } catch (ExecutionException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   282
                    if (e.getCause() instanceof SecurityException) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   283
                        throw (SecurityException)e.getCause();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   284
                    } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   285
                        throw new RuntimeException(e);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   286
                    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   287
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   288
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   289
            // (15) check that user provided unprivileged code running on a worker
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   290
            //      thread does not gain ungranted privileges.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   291
            test(false, () -> { //Policy 12
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   292
                URI u = URI.create("http://127.0.0.1:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   293
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   294
                HttpResponse.BodyHandler<String> sth = asString();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   295
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   296
                CompletableFuture<HttpResponse<String>> cf =
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   297
                    client.sendAsync(request, new HttpResponse.BodyHandler<String>() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   298
                        @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   299
                        public HttpResponse.BodySubscriber<String> apply(int status, HttpHeaders responseHeaders)  {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   300
                            final HttpResponse.BodySubscriber<String> stproc = sth.apply(status, responseHeaders);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   301
                            return new HttpResponse.BodySubscriber<String>() {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   302
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   303
                                public CompletionStage<String> getBody() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   304
                                    return stproc.getBody();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   305
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   306
                                @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   307
                                public void onNext(List<ByteBuffer> item) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   308
                                    SecurityManager sm = System.getSecurityManager();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   309
                                    // should succeed.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   310
                                    sm.checkPermission(new RuntimePermission("foobar"));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   311
                                    // do some mischief here
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   312
                                    System.setSecurityManager(null);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   313
                                    System.setSecurityManager(sm);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   314
                                    // problem if we get this far
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   315
                                    stproc.onNext(item);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   316
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   317
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   318
                                public void onSubscribe(Flow.Subscription subscription) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   319
                                    stproc.onSubscribe(subscription);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   320
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   321
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   322
                                public void onError(Throwable throwable) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   323
                                    stproc.onError(throwable);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   324
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   325
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   326
                                public void onComplete() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   327
                                    stproc.onComplete();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   328
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   329
                            };
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   330
                        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   331
                    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   332
                );
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   333
                try {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   334
                    cf.join();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   335
                } catch (CompletionException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   336
                    Throwable t = e.getCause();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   337
                    if (t instanceof SecurityException)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   338
                        throw (SecurityException)t;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   339
                    else if ((t instanceof IOException)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   340
                              && (t.getCause() instanceof SecurityException))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   341
                        throw ((SecurityException)t.getCause());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   342
                    else
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   343
                        throw new RuntimeException(t);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   344
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   345
            })
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   346
        };
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   347
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   348
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   349
    private static void directProxyTest(int proxyPort, boolean samePort)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   350
        throws IOException, InterruptedException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   351
    {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   352
        Object proxy = null;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   353
        try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   354
            proxy = getProxy(proxyPort, true);
37897
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   355
        } catch (BindException e) {
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   356
            System.out.println("Bind failed");
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   357
            throw e;
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   358
        } catch (Throwable ee) {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   359
            throw new RuntimeException(ee);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   360
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   361
        System.out.println("Proxy port = " + proxyPort);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   362
        if (!samePort)
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   363
            proxyPort++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   364
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   365
        InetSocketAddress addr = new InetSocketAddress("127.0.0.1", proxyPort);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   366
        HttpClient cl = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   367
                                    .proxy(ProxySelector.of(addr))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   368
                                    .build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   369
        clients.add(cl);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   370
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   371
        URI u = URI.create("http://127.0.0.1:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   372
        HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   373
                                         .headers("X-Foo", "bar", "X-Bar", "foo")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   374
                                         .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   375
        HttpResponse<?> response = cl.send(request, asString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   376
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   377
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   378
    static void runtest(Test r, String policy, boolean succeeds) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   379
        System.out.println("Using policy file: " + policy);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   380
        try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   381
            r.execute();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   382
            if (!succeeds) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   383
                System.out.println("FAILED: expected security exception");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   384
                throw new RuntimeException("FAILED: expected security exception\"");
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   385
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   386
            System.out.println (policy + " succeeded as expected");
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   387
        } catch (BindException e) {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   388
            System.exit(10);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   389
        } catch (SecurityException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   390
            if (succeeds) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   391
                System.out.println("FAILED");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   392
                throw new RuntimeException(e);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   393
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   394
            System.out.println (policy + " threw exception as expected");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   395
        } catch (IOException | InterruptedException ee) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   396
            throw new RuntimeException(ee);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   397
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   398
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   399
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   400
    public static void main(String[] args) throws Exception {
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   401
        try {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   402
            initServer();
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   403
            setupProxy();
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   404
        } catch (BindException e) {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   405
            System.exit(10);
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   406
        }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   407
        fileroot = System.getProperty ("test.src")+ "/docs";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   408
        int testnum = Integer.parseInt(args[0]);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   409
        String policy = args[0];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   410
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   411
        client = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   412
                           .followRedirects(HttpClient.Redirect.ALWAYS)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   413
                           .build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   414
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   415
        clients.add(client);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   416
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   417
        try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   418
            setupTests();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   419
            TestAndResult tr = tests[testnum];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   420
            runtest(tr.test, policy, tr.result);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   421
        } finally {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   422
            s1.stop(0);
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   423
            executor.shutdownNow();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   424
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   425
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   426
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   427
    public static void initServer() throws Exception {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   428
        String portstring = System.getProperty("port.number");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   429
        port = portstring != null ? Integer.parseInt(portstring) : 0;
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   430
        portstring = System.getProperty("port.number1");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   431
        proxyPort = portstring != null ? Integer.parseInt(portstring) : 0;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   432
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   433
        Logger logger = Logger.getLogger("com.sun.net.httpserver");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   434
        ConsoleHandler ch = new ConsoleHandler();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   435
        logger.setLevel(Level.ALL);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   436
        ch.setLevel(Level.ALL);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   437
        logger.addHandler(ch);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   438
        String root = System.getProperty ("test.src")+ "/docs";
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   439
        InetSocketAddress addr = new InetSocketAddress (port);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   440
        s1 = HttpServer.create (addr, 0);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   441
        if (s1 instanceof HttpsServer) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   442
            throw new RuntimeException ("should not be httpsserver");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   443
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   444
        HttpHandler h = new FileServerHandler (root);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   445
        HttpContext c = s1.createContext ("/files", h);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   446
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   447
        HttpHandler h1 = new RedirectHandler ("/redirect");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   448
        HttpContext c1 = s1.createContext ("/redirect", h1);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   449
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   450
        executor = Executors.newCachedThreadPool();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   451
        s1.setExecutor (executor);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   452
        s1.start();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   453
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   454
        if (port == 0)
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   455
            port = s1.getAddress().getPort();
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   456
        else {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   457
            if (s1.getAddress().getPort() != port)
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   458
                throw new RuntimeException("Error wrong port");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   459
            System.out.println("Port was assigned by Driver");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   460
        }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   461
        System.out.println("HTTP server port = " + port);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   462
        httproot = "http://127.0.0.1:" + port + "/files/";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   463
        redirectroot = "http://127.0.0.1:" + port + "/redirect/";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   464
        uri = new URI(httproot);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   465
        fileuri = httproot + "foo.txt";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   466
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   467
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   468
    static class RedirectHandler implements HttpHandler {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   469
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   470
        String root;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   471
        int count = 0;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   472
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   473
        RedirectHandler(String root) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   474
            this.root = root;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   475
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   476
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   477
        synchronized int count() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   478
            return count;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   479
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   480
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   481
        synchronized void increment() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   482
            count++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   483
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   484
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   485
        @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   486
        public synchronized void handle(HttpExchange t)
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   487
                throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   488
            byte[] buf = new byte[2048];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   489
            System.out.println("Server: " + t.getRequestURI());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   490
            try (InputStream is = t.getRequestBody()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   491
                while (is.read(buf) != -1) ;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   492
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   493
            increment();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   494
            if (count() == 1) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   495
                Headers map = t.getResponseHeaders();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   496
                String redirect = "/redirect/bar.txt";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   497
                map.add("Location", redirect);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   498
                t.sendResponseHeaders(301, -1);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   499
                t.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   500
            } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   501
                String response = "Hello world";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   502
                t.sendResponseHeaders(200, response.length());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   503
                OutputStream os = t.getResponseBody();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   504
                os.write(response.getBytes(StandardCharsets.ISO_8859_1));
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   505
                t.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   506
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   507
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   508
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   509
}