test/jdk/java/net/httpclient/ProxyAuthTest.java
author chegar
Sun, 05 Nov 2017 21:19:55 +0000
branchhttp-client-branch
changeset 55764 34d7cc00f87a
parent 47216 71c04702a3d5
child 49765 ee6f7a61f3a5
child 56082 1da51fab3032
permissions -rw-r--r--
http-client-branch: WebSocket permission checks, test updates, and more
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     1
/*
55764
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     4
 *
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    10
 *
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    15
 * accompanied this code).
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    16
 *
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    20
 *
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 42460
diff changeset
    23
 * questions.
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    24
 */
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    25
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    26
/*
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    27
 * @test
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    28
 * @bug 8163561
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    29
 * @modules java.base/sun.net.www
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    30
 *          jdk.incubator.httpclient
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    31
 * @summary Verify that Proxy-Authenticate header is correctly handled
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    32
 * @run main/othervm ProxyAuthTest
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    33
 */
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    34
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    35
import java.io.BufferedWriter;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    36
import java.io.IOException;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    37
import java.io.InputStream;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    38
import java.io.OutputStream;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    39
import java.io.OutputStreamWriter;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    40
import java.io.PrintWriter;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    41
import java.net.Authenticator;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    42
import java.net.InetSocketAddress;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    43
import java.net.PasswordAuthentication;
55764
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
    44
import java.net.Proxy;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    45
import java.net.ProxySelector;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    46
import java.net.ServerSocket;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    47
import java.net.Socket;
55764
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
    48
import java.net.SocketAddress;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    49
import java.net.URI;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    50
import jdk.incubator.http.HttpClient;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    51
import jdk.incubator.http.HttpRequest;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    52
import jdk.incubator.http.HttpResponse;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    53
import java.util.Base64;
55764
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
    54
import java.util.List;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    55
import sun.net.www.MessageHeader;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    56
import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    57
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    58
public class ProxyAuthTest {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    59
    private static final String AUTH_USER = "user";
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    60
    private static final String AUTH_PASSWORD = "password";
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    61
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    62
    public static void main(String[] args) throws Exception {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    63
        try (ServerSocket ss = new ServerSocket(0)) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    64
            int port = ss.getLocalPort();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    65
            MyProxy proxy = new MyProxy(ss);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    66
            (new Thread(proxy)).start();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    67
            System.out.println("Proxy listening port " + port);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    68
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    69
            Auth auth = new Auth();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    70
            InetSocketAddress paddr = new InetSocketAddress("localhost", port);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    71
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    72
            URI uri = new URI("http://www.google.ie/");
55764
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
    73
            CountingProxySelector ps = CountingProxySelector.of(paddr);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    74
            HttpClient client = HttpClient.newBuilder()
55764
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
    75
                                          .proxy(ps)
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    76
                                          .authenticator(auth)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    77
                                          .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    78
            HttpRequest req = HttpRequest.newBuilder(uri).GET().build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    79
            HttpResponse<?> resp = client.sendAsync(req, discard(null)).get();
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    80
            if (resp.statusCode() != 404) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    81
                throw new RuntimeException("Unexpected status code: " + resp.statusCode());
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    82
            }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    83
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    84
            if (auth.isCalled) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    85
                System.out.println("Authenticator is called");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    86
            } else {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    87
                throw new RuntimeException("Authenticator is not called");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    88
            }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    89
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    90
            if (!proxy.matched) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    91
                throw new RuntimeException("Proxy authentication failed");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    92
            }
55764
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
    93
            if (ps.count() > 1) {
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
    94
                throw new RuntimeException("CountingProxySelector. Expected 1, got " + ps.count());
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
    95
            }
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    96
        }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    97
    }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    98
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    99
    static class Auth extends Authenticator {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   100
        private volatile boolean isCalled;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   101
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   102
        @Override
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   103
        protected PasswordAuthentication getPasswordAuthentication() {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   104
            System.out.println("scheme: " + this.getRequestingScheme());
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   105
            isCalled = true;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   106
            return new PasswordAuthentication(AUTH_USER,
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   107
                    AUTH_PASSWORD.toCharArray());
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   108
        }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   109
    }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   110
55764
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   111
    /**
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   112
     * A Proxy Selector that wraps a ProxySelector.of(), and counts the number
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   113
     * of times its select method has been invoked. This can be used to ensure
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   114
     * that the Proxy Selector is invoked only once per HttpClient.sendXXX
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   115
     * invocation.
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   116
     */
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   117
    static class CountingProxySelector extends ProxySelector {
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   118
        private final ProxySelector proxySelector;
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   119
        private volatile int count; // 0
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   120
        private CountingProxySelector(InetSocketAddress proxyAddress) {
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   121
            proxySelector = ProxySelector.of(proxyAddress);
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   122
        }
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   123
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   124
        public static CountingProxySelector of(InetSocketAddress proxyAddress) {
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   125
            return new CountingProxySelector(proxyAddress);
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   126
        }
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   127
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   128
        int count() { return count; }
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   129
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   130
        @Override
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   131
        public List<Proxy> select(URI uri) {
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   132
            count++;
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   133
            return proxySelector.select(uri);
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   134
        }
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   135
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   136
        @Override
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   137
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   138
            proxySelector.connectFailed(uri, sa, ioe);
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   139
        }
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   140
    }
34d7cc00f87a http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents: 47216
diff changeset
   141
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   142
    static class MyProxy implements Runnable {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   143
        final ServerSocket ss;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   144
        private volatile boolean matched;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   145
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   146
        MyProxy(ServerSocket ss) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   147
            this.ss = ss;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   148
        }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   149
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   150
        public void run() {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   151
            for (int i = 0; i < 2; i++) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   152
                try (Socket s = ss.accept();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   153
                     InputStream in = s.getInputStream();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   154
                     OutputStream os = s.getOutputStream();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   155
                     BufferedWriter writer = new BufferedWriter(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   156
                             new OutputStreamWriter(os));
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   157
                     PrintWriter out = new PrintWriter(writer);) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   158
                    MessageHeader headers = new MessageHeader(in);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   159
                    System.out.println("Proxy: received " + headers);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   160
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   161
                    String authInfo = headers.findValue("Proxy-Authorization");
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   162
                    if (authInfo != null) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   163
                        authenticate(authInfo);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   164
                        out.print("HTTP/1.1 404 Not found\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   165
                        out.print("\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   166
                        System.out.println("Proxy: 404");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   167
                        out.flush();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   168
                    } else {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   169
                        out.print("HTTP/1.1 407 Proxy Authorization Required\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   170
                        out.print(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   171
                                "Proxy-Authenticate: Basic realm=\"a fake realm\"\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   172
                        out.print("\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   173
                        System.out.println("Proxy: Authorization required");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   174
                        out.flush();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   175
                    }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   176
                } catch (IOException x) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   177
                    System.err.println("Unexpected IOException from proxy.");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   178
                    x.printStackTrace();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   179
                    break;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   180
                }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   181
            }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   182
        }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   183
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   184
        private void authenticate(String authInfo) throws IOException {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   185
            try {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   186
                authInfo.trim();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   187
                int ind = authInfo.indexOf(' ');
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   188
                String recvdUserPlusPass = authInfo.substring(ind + 1).trim();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   189
                // extract encoded username:passwd
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   190
                String value = new String(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   191
                        Base64.getMimeDecoder().decode(recvdUserPlusPass));
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   192
                String userPlusPassword = AUTH_USER + ":" + AUTH_PASSWORD;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   193
                if (userPlusPassword.equals(value)) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   194
                    matched = true;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   195
                    System.out.println("Proxy: client authentication successful");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   196
                } else {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   197
                    System.err.println(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   198
                            "Proxy: client authentication failed, expected ["
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   199
                                    + userPlusPassword + "], actual [" + value
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   200
                                    + "]");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   201
                }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   202
            } catch (Exception e) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   203
                throw new IOException(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   204
                        "Proxy received invalid Proxy-Authorization value: "
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   205
                                + authInfo);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   206
            }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   207
        }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   208
    }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   209
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   210
}