test/jdk/java/net/httpclient/ProxyAuthTest.java
author chegar
Fri, 02 Nov 2018 16:11:29 +0000
changeset 52387 8c0b1894d524
parent 49765 ee6f7a61f3a5
permissions -rw-r--r--
8213296: Fix legal headers in test/jdk/java/net Reviewed-by: alanb, dfuchs
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
/*
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
     2
 * Copyright (c) 2016, 2018, 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
52387
8c0b1894d524 8213296: Fix legal headers in test/jdk/java/net
chegar
parents: 49765
diff changeset
     7
 * published by the Free Software Foundation.
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     8
 *
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     9
 * 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
    10
 * 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
    11
 * 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
    12
 * 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
    13
 * accompanied this code).
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    14
 *
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    15
 * 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
    16
 * 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
    17
 * 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
    18
 *
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    19
 * 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
    20
 * 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
    21
 * questions.
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    22
 */
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    23
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
 * @test
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    26
 * @bug 8163561
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    27
 * @modules java.base/sun.net.www
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    28
 *          java.net.http
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    29
 * @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
    30
 * @run main/othervm ProxyAuthTest
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    31
 */
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    32
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    33
import java.io.BufferedWriter;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    34
import java.io.IOException;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    35
import java.io.InputStream;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    36
import java.io.OutputStream;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    37
import java.io.OutputStreamWriter;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    38
import java.io.PrintWriter;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    39
import java.net.Authenticator;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    40
import java.net.InetAddress;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    41
import java.net.InetSocketAddress;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    42
import java.net.PasswordAuthentication;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    43
import java.net.Proxy;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    44
import java.net.ProxySelector;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    45
import java.net.ServerSocket;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    46
import java.net.Socket;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    47
import java.net.SocketAddress;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    48
import java.net.URI;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    49
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    50
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    51
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    52
import java.net.http.HttpResponse.BodyHandlers;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    53
import java.util.Base64;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
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;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    56
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    57
public class ProxyAuthTest {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    58
    private static final String AUTH_USER = "user";
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    59
    private static final String AUTH_PASSWORD = "password";
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    60
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    61
    public static void main(String[] args) throws Exception {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    62
        try (ServerSocket ss = new ServerSocket()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    63
            ss.setReuseAddress(false);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    64
            ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    65
            int port = ss.getLocalPort();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    66
            MyProxy proxy = new MyProxy(ss);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    67
            (new Thread(proxy)).start();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    68
            System.out.println("Proxy listening port " + port);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    69
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    70
            Auth auth = new Auth();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    71
            InetSocketAddress paddr = new InetSocketAddress(InetAddress.getLoopbackAddress(), port);
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    72
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    73
            URI uri = new URI("http://www.google.ie/");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    74
            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
    75
            HttpClient client = HttpClient.newBuilder()
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    76
                                          .proxy(ps)
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    77
                                          .authenticator(auth)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    78
                                          .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    79
            HttpRequest req = HttpRequest.newBuilder(uri).GET().build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    80
            HttpResponse<?> resp = client.sendAsync(req, BodyHandlers.discarding()).get();
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    81
            if (resp.statusCode() != 404) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    82
                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
    83
            }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    84
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    85
            if (auth.isCalled) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    86
                System.out.println("Authenticator is called");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    87
            } else {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    88
                throw new RuntimeException("Authenticator is not called");
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
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    91
            if (!proxy.matched) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    92
                throw new RuntimeException("Proxy authentication failed");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    93
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    94
            if (ps.count() > 1) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    95
                throw new RuntimeException("CountingProxySelector. Expected 1, got " + ps.count());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    96
            }
40560
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
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   100
    static class Auth extends Authenticator {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   101
        private volatile boolean isCalled;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   102
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   103
        @Override
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   104
        protected PasswordAuthentication getPasswordAuthentication() {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   105
            System.out.println("scheme: " + this.getRequestingScheme());
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   106
            isCalled = true;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   107
            return new PasswordAuthentication(AUTH_USER,
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   108
                    AUTH_PASSWORD.toCharArray());
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
    }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   111
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   112
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   113
     * A Proxy Selector that wraps a ProxySelector.of(), and counts the number
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   114
     * of times its select method has been invoked. This can be used to ensure
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   115
     * that the Proxy Selector is invoked only once per HttpClient.sendXXX
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   116
     * invocation.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   117
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   118
    static class CountingProxySelector extends ProxySelector {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   119
        private final ProxySelector proxySelector;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   120
        private volatile int count; // 0
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   121
        private CountingProxySelector(InetSocketAddress proxyAddress) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   122
            proxySelector = ProxySelector.of(proxyAddress);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   123
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   124
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   125
        public static CountingProxySelector of(InetSocketAddress proxyAddress) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   126
            return new CountingProxySelector(proxyAddress);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   127
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   128
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   129
        int count() { return count; }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   130
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   131
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   132
        public List<Proxy> select(URI uri) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   133
            count++;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   134
            return proxySelector.select(uri);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   135
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   136
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   137
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   138
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   139
            proxySelector.connectFailed(uri, sa, ioe);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   140
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   141
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   142
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   143
    static class MyProxy implements Runnable {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   144
        final ServerSocket ss;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   145
        private volatile boolean matched;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   146
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   147
        MyProxy(ServerSocket ss) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   148
            this.ss = ss;
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
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   151
        public void run() {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   152
            for (int i = 0; i < 2; i++) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   153
                try (Socket s = ss.accept();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   154
                     InputStream in = s.getInputStream();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   155
                     OutputStream os = s.getOutputStream();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   156
                     BufferedWriter writer = new BufferedWriter(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   157
                             new OutputStreamWriter(os));
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   158
                     PrintWriter out = new PrintWriter(writer);) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   159
                    MessageHeader headers = new MessageHeader(in);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   160
                    System.out.println("Proxy: received " + headers);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   161
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   162
                    String authInfo = headers.findValue("Proxy-Authorization");
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   163
                    if (authInfo != null) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   164
                        authenticate(authInfo);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   165
                        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
   166
                        out.print("\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   167
                        System.out.println("Proxy: 404");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   168
                        out.flush();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   169
                    } else {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   170
                        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
   171
                        out.print(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   172
                                "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
   173
                        out.print("\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   174
                        System.out.println("Proxy: Authorization required");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   175
                        out.flush();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   176
                    }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   177
                } catch (IOException x) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   178
                    System.err.println("Unexpected IOException from proxy.");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   179
                    x.printStackTrace();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   180
                    break;
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
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   185
        private void authenticate(String authInfo) throws IOException {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   186
            try {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   187
                authInfo.trim();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   188
                int ind = authInfo.indexOf(' ');
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   189
                String recvdUserPlusPass = authInfo.substring(ind + 1).trim();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   190
                // extract encoded username:passwd
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   191
                String value = new String(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   192
                        Base64.getMimeDecoder().decode(recvdUserPlusPass));
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   193
                String userPlusPassword = AUTH_USER + ":" + AUTH_PASSWORD;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   194
                if (userPlusPassword.equals(value)) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   195
                    matched = true;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   196
                    System.out.println("Proxy: client authentication successful");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   197
                } else {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   198
                    System.err.println(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   199
                            "Proxy: client authentication failed, expected ["
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   200
                                    + userPlusPassword + "], actual [" + value
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
                }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   203
            } catch (Exception e) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   204
                throw new IOException(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   205
                        "Proxy received invalid Proxy-Authorization value: "
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   206
                                + authInfo);
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
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   211
}