test/jdk/java/net/httpclient/security/Security.java
author chegar
Thu, 08 Mar 2018 17:42:16 +0000
branchhttp-client-branch
changeset 56265 ec34ae013fbe
parent 56167 96fa4f49a9ff
child 56369 24a8fafec3ff
permissions -rw-r--r--
http-client-branch: tests should bind to the loopback only
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
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    27
 * @modules java.net.http
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;
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
    64
import java.net.InetAddress;
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    65
import java.net.InetSocketAddress;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    66
import java.net.ProxySelector;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    67
import java.net.URI;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    68
import java.net.URLClassLoader;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    69
import java.net.URL;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    70
import java.net.http.HttpHeaders;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    71
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    72
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    73
import java.net.http.HttpResponse;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    74
import java.nio.charset.StandardCharsets;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    75
import java.nio.file.Files;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    76
import java.nio.file.Path;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    77
import java.nio.ByteBuffer;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    78
import java.nio.file.Paths;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    79
import java.nio.file.StandardCopyOption;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    80
import java.util.LinkedList;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    81
import java.util.List;
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    82
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
    83
import java.util.concurrent.CompletionException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    84
import java.util.concurrent.CompletionStage;
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    85
import java.util.concurrent.ExecutionException;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    86
import java.util.concurrent.Executors;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    87
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
    88
import java.util.concurrent.Flow;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    89
import java.util.logging.ConsoleHandler;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    90
import java.util.logging.Level;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    91
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
    92
import java.lang.reflect.InvocationTargetException;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
    93
import static java.net.http.HttpResponse.BodyHandlers.ofString;
36131
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
/**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    96
 * Security checks test
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    97
 */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    98
public class Security {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    99
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   100
    static HttpServer s1 = null;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   101
    static ExecutorService executor=null;
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   102
    static int port, proxyPort;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   103
    static HttpClient client;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   104
    static String httproot, fileuri, fileroot, redirectroot;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   105
    static List<HttpClient> clients = new LinkedList<>();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   106
    static URI uri;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   107
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   108
    interface Test {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   109
        void execute() throws IOException, InterruptedException;
36131
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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   112
    static class TestAndResult {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   113
        Test test;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   114
        boolean result;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   115
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   116
        TestAndResult (Test t, boolean result) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   117
            this.test = t;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   118
            this.result = result;
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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   122
    static TestAndResult test(boolean result, Test t) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   123
        return new TestAndResult(t, result);
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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   126
    static TestAndResult[] tests;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   127
    static String testclasses;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   128
    static File subdir;
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
    /**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   131
     * The ProxyServer class is compiled by jtreg, but we want to
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   132
     * 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
   133
     * 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
   134
     * protection domain and security permissions.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   135
     *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   136
     * 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
   137
     */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   138
    static void setupProxy() throws IOException, ClassNotFoundException, NoSuchMethodException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   139
        testclasses = System.getProperty("test.classes");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   140
        subdir = new File (testclasses, "proxydir");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   141
        subdir.mkdir();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   142
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   143
        movefile("ProxyServer.class");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   144
        movefile("ProxyServer$Connection.class");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   145
        movefile("ProxyServer$1.class");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   146
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   147
        URL url = subdir.toURL();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   148
        System.out.println("URL for class loader = " + url);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   149
        URLClassLoader urlc = new URLClassLoader(new URL[] {url});
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   150
        proxyClass = Class.forName("ProxyServer", true, urlc);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   151
        proxyConstructor = proxyClass.getConstructor(Integer.class, Boolean.class);
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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   154
    static void movefile(String f) throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   155
        Path src = Paths.get(testclasses, f);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   156
        Path dest = subdir.toPath().resolve(f);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   157
        if (!dest.toFile().exists()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   158
            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
   159
            Files.move(src, dest,  StandardCopyOption.REPLACE_EXISTING);
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   160
        } else if (src.toFile().exists()) {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   161
            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
   162
            Files.delete(src);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   163
        } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   164
            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
   165
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   166
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   167
37897
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   168
    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
   169
        try {
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   170
            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
   171
        } catch (InvocationTargetException e) {
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   172
            throw e.getTargetException();
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   173
        }
36131
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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   176
    static Class<?> proxyClass;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   177
    static Constructor<?> proxyConstructor;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   178
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   179
    static void setupTests() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   180
        tests = new TestAndResult[]{
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   181
            // (0) policy does not have permission for file. Should fail
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   182
            test(false, () -> { // Policy 0
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   183
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   184
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   185
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   186
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   187
            // (1) policy has permission for file URL
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   188
            test(true, () -> { //Policy 1
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   189
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   190
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   191
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   192
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   193
            // (2) policy has permission for all file URLs under /files
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   194
            test(true, () -> { // Policy 2
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   195
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   196
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   197
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   198
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   199
            // (3) policy has permission for first URL but not redirected URL
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   200
            test(false, () -> { // Policy 3
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   201
                URI u = URI.create("http://localhost:" + port + "/redirect/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   202
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   203
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   204
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   205
            // (4) policy has permission for both first URL and redirected URL
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   206
            test(true, () -> { // Policy 4
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   207
                URI u = URI.create("http://localhost:" + port + "/redirect/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   208
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   209
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   210
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   211
            // (5) policy has permission for redirected but not first URL
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   212
            test(false, () -> { // Policy 5
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   213
                URI u = URI.create("http://localhost:" + port + "/redirect/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   214
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   215
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   216
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   217
            // (6) policy has permission for file URL, but not method
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   218
            test(false, () -> { //Policy 6
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   219
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   220
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   221
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   222
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   223
            // (7) policy has permission for file URL, method, but not header
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   224
            test(false, () -> { //Policy 7
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   225
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   226
                HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   227
                                                 .header("X-Foo", "bar")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   228
                                                 .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   229
                                                 .build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   230
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   231
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   232
            // (8) policy has permission for file URL, method and header
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   233
            test(true, () -> { //Policy 8
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   234
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   235
                HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   236
                                                 .header("X-Foo", "bar")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   237
                                                 .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   238
                                                 .build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   239
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   240
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   241
            // (9) policy has permission for file URL, method and header
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   242
            test(true, () -> { //Policy 9
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   243
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   244
                HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   245
                                                 .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
   246
                                                 .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   247
                                                 .build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   248
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   249
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   250
            // (10) policy has permission for destination URL but not for proxy
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   251
            test(false, () -> { //Policy 10
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   252
                directProxyTest(proxyPort, true);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   253
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   254
            // (11) policy has permission for both destination URL and proxy
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   255
            test(true, () -> { //Policy 11
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   256
                directProxyTest(proxyPort, true);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   257
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   258
            // (12) policy has permission for both destination URL and proxy
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   259
            test(false, () -> { //Policy 11
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   260
                directProxyTest(proxyPort, false);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   261
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   262
            // (13) async version of test 0
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   263
            test(false, () -> { // Policy 0
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   264
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   265
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   266
                try {
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   267
                    HttpResponse<?> response = client.sendAsync(request, ofString()).get();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   268
                } catch (ExecutionException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   269
                    if (e.getCause() instanceof SecurityException) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   270
                        throw (SecurityException)e.getCause();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   271
                    } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   272
                        throw new RuntimeException(e);
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
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   276
            // (14) async version of test 1
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   277
            test(true, () -> { //Policy 1
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   278
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   279
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   280
                try {
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   281
                    HttpResponse<?> response = client.sendAsync(request, ofString()).get();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   282
                } catch (ExecutionException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   283
                    if (e.getCause() instanceof SecurityException) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   284
                        throw (SecurityException)e.getCause();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   285
                    } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   286
                        throw new RuntimeException(e);
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
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   290
            // (15) check that user provided unprivileged code running on a worker
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   291
            //      thread does not gain ungranted privileges.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   292
            test(false, () -> { //Policy 12
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   293
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   294
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   295
                HttpResponse.BodyHandler<String> sth = ofString();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   296
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   297
                CompletableFuture<HttpResponse<String>> cf =
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   298
                    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
   299
                        @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   300
                        public HttpResponse.BodySubscriber<String> apply(int status, HttpHeaders responseHeaders)  {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   301
                            final HttpResponse.BodySubscriber<String> stproc = sth.apply(status, responseHeaders);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   302
                            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
   303
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   304
                                public CompletionStage<String> getBody() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   305
                                    return stproc.getBody();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   306
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   307
                                @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   308
                                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
   309
                                    SecurityManager sm = System.getSecurityManager();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   310
                                    // should succeed.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   311
                                    sm.checkPermission(new RuntimePermission("foobar"));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   312
                                    // do some mischief here
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   313
                                    System.setSecurityManager(null);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   314
                                    System.setSecurityManager(sm);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   315
                                    // 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
   316
                                    stproc.onNext(item);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   317
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   318
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   319
                                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
   320
                                    stproc.onSubscribe(subscription);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   321
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   322
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   323
                                public void onError(Throwable throwable) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   324
                                    stproc.onError(throwable);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   325
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   326
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   327
                                public void onComplete() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   328
                                    stproc.onComplete();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   329
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   330
                            };
36131
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
                );
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   334
                try {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   335
                    cf.join();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   336
                } catch (CompletionException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   337
                    Throwable t = e.getCause();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   338
                    if (t instanceof SecurityException)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   339
                        throw (SecurityException)t;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   340
                    else if ((t instanceof IOException)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   341
                              && (t.getCause() instanceof SecurityException))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   342
                        throw ((SecurityException)t.getCause());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   343
                    else
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   344
                        throw new RuntimeException(t);
36131
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
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   349
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   350
    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
   351
        throws IOException, InterruptedException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   352
    {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   353
        Object proxy = null;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   354
        try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   355
            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
   356
        } catch (BindException e) {
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   357
            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
   358
            throw e;
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   359
        } catch (Throwable ee) {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   360
            throw new RuntimeException(ee);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   361
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   362
        System.out.println("Proxy port = " + proxyPort);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   363
        if (!samePort)
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   364
            proxyPort++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   365
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   366
        InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(),
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   367
                                                       proxyPort);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   368
        HttpClient cl = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   369
                                    .proxy(ProxySelector.of(addr))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   370
                                    .build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   371
        clients.add(cl);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   372
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   373
        URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   374
        HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   375
                                         .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
   376
                                         .build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   377
        HttpResponse<?> response = cl.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   378
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   379
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   380
    static void runtest(Test r, String policy, boolean succeeds) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   381
        System.out.println("Using policy file: " + policy);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   382
        try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   383
            r.execute();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   384
            if (!succeeds) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   385
                System.out.println("FAILED: expected security exception");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   386
                throw new RuntimeException("FAILED: expected security exception\"");
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   387
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   388
            System.out.println (policy + " succeeded as expected");
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   389
        } catch (BindException e) {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   390
            System.exit(10);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   391
        } catch (SecurityException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   392
            if (succeeds) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   393
                System.out.println("FAILED");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   394
                throw new RuntimeException(e);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   395
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   396
            System.out.println (policy + " threw exception as expected");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   397
        } catch (IOException | InterruptedException ee) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   398
            throw new RuntimeException(ee);
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
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   401
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   402
    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
   403
        try {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   404
            initServer();
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   405
            setupProxy();
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   406
        } catch (BindException e) {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   407
            System.exit(10);
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   408
        }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   409
        fileroot = System.getProperty ("test.src")+ "/docs";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   410
        int testnum = Integer.parseInt(args[0]);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   411
        String policy = args[0];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   412
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   413
        client = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   414
                           .followRedirects(HttpClient.Redirect.ALWAYS)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   415
                           .build();
36131
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
        clients.add(client);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   418
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   419
        try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   420
            setupTests();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   421
            TestAndResult tr = tests[testnum];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   422
            runtest(tr.test, policy, tr.result);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   423
        } finally {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   424
            s1.stop(0);
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   425
            executor.shutdownNow();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   426
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   427
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   428
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   429
    public static void initServer() throws Exception {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   430
        String portstring = System.getProperty("port.number");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   431
        port = portstring != null ? Integer.parseInt(portstring) : 0;
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   432
        portstring = System.getProperty("port.number1");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   433
        proxyPort = portstring != null ? Integer.parseInt(portstring) : 0;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   434
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   435
        Logger logger = Logger.getLogger("com.sun.net.httpserver");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   436
        ConsoleHandler ch = new ConsoleHandler();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   437
        logger.setLevel(Level.ALL);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   438
        ch.setLevel(Level.ALL);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   439
        logger.addHandler(ch);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   440
        String root = System.getProperty ("test.src")+ "/docs";
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   441
        InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), port);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   442
        s1 = HttpServer.create (addr, 0);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   443
        if (s1 instanceof HttpsServer) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   444
            throw new RuntimeException ("should not be httpsserver");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   445
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   446
        HttpHandler h = new FileServerHandler (root);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   447
        HttpContext c = s1.createContext ("/files", h);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   448
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   449
        HttpHandler h1 = new RedirectHandler ("/redirect");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   450
        HttpContext c1 = s1.createContext ("/redirect", h1);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   451
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   452
        executor = Executors.newCachedThreadPool();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   453
        s1.setExecutor (executor);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   454
        s1.start();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   455
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   456
        if (port == 0)
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   457
            port = s1.getAddress().getPort();
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   458
        else {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   459
            if (s1.getAddress().getPort() != port)
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   460
                throw new RuntimeException("Error wrong port");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   461
            System.out.println("Port was assigned by Driver");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   462
        }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   463
        System.out.println("HTTP server port = " + port);
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   464
        httproot = "http://localhost:" + port + "/files/";
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   465
        redirectroot = "http://localhost:" + port + "/redirect/";
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   466
        uri = new URI(httproot);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   467
        fileuri = httproot + "foo.txt";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   468
    }
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
    static class RedirectHandler implements HttpHandler {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   471
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   472
        String root;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   473
        int count = 0;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   474
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   475
        RedirectHandler(String root) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   476
            this.root = root;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   477
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   478
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   479
        synchronized int count() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   480
            return count;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   481
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   482
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   483
        synchronized void increment() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   484
            count++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   485
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   486
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   487
        @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   488
        public synchronized void handle(HttpExchange t)
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   489
                throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   490
            byte[] buf = new byte[2048];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   491
            System.out.println("Server: " + t.getRequestURI());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   492
            try (InputStream is = t.getRequestBody()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   493
                while (is.read(buf) != -1) ;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   494
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   495
            increment();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   496
            if (count() == 1) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   497
                Headers map = t.getResponseHeaders();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   498
                String redirect = "/redirect/bar.txt";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   499
                map.add("Location", redirect);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   500
                t.sendResponseHeaders(301, -1);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   501
                t.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   502
            } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   503
                String response = "Hello world";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   504
                t.sendResponseHeaders(200, response.length());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   505
                OutputStream os = t.getResponseBody();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   506
                os.write(response.getBytes(StandardCharsets.ISO_8859_1));
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   507
                t.close();
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
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   510
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   511
}