test/jdk/java/net/httpclient/ProxyServer.java
author chegar
Tue, 29 Jan 2019 16:12:12 +0000
changeset 53551 ab949b3e39bc
parent 53521 41fa3e6f2785
permissions -rw-r--r--
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently Reviewed-by: dfuchs
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
/*
53521
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
     2
 * Copyright (c) 2015, 2019, 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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    21
 * questions.
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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    24
import java.net.*;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    25
import java.io.*;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    26
import java.util.*;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    27
import java.security.*;
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
    28
import java.util.concurrent.CopyOnWriteArrayList;
53521
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    29
import static java.nio.charset.StandardCharsets.UTF_8;
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    30
import static java.util.Arrays.asList;
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    31
import static java.util.stream.Collectors.toList;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    32
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    33
/**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    34
 * A minimal proxy server that supports CONNECT tunneling. It does not do
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    35
 * any header transformations. In future this could be added.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    36
 * Two threads are created per client connection. So, it's not
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    37
 * intended for large numbers of parallel connections.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    38
 */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    39
public class ProxyServer extends Thread implements Closeable {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    40
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    41
    ServerSocket listener;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    42
    int port;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    43
    volatile boolean debug;
53521
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    44
    private final Credentials credentials;  // may be null
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    45
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    46
    private static class Credentials {
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    47
        private final String name;
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    48
        private final String password;
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    49
        private Credentials(String name, String password) {
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    50
            this.name = name;
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    51
            this.password = password;
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    52
        }
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    53
        public String name() { return name; }
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    54
        public String password() { return password; }
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    55
    }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    56
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    57
    /**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    58
     * Create proxy on port (zero means don't care). Call getPort()
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    59
     * to get the assigned port.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    60
     */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    61
    public ProxyServer(Integer port) throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    62
        this(port, false);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    63
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    64
53521
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    65
    public ProxyServer(Integer port,
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    66
                       Boolean debug,
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    67
                       String username,
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    68
                       String password)
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    69
        throws IOException
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    70
    {
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    71
        this(port, debug, new Credentials(username, password));
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    72
    }
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    73
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    74
    public ProxyServer(Integer port,
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    75
                       Boolean debug)
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    76
        throws IOException
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    77
    {
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    78
        this(port, debug, null);
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    79
    }
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    80
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    81
    public ProxyServer(Integer port,
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    82
                       Boolean debug,
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    83
                       Credentials credentials)
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    84
        throws IOException
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    85
    {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    86
        this.debug = debug;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    87
        listener = new ServerSocket();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    88
        listener.setReuseAddress(false);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    89
        listener.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), port));
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    90
        this.port = listener.getLocalPort();
53521
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    91
        this.credentials = credentials;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    92
        setName("ProxyListener");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    93
        setDaemon(true);
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
    94
        connections = new CopyOnWriteArrayList<Connection>();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    95
        start();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    96
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    97
53521
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    98
    public ProxyServer(String s) {
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
    99
        credentials = null;
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   100
        connections = new CopyOnWriteArrayList<Connection>();
53521
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   101
    }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   102
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   103
    /**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   104
     * Returns the port number this proxy is listening on
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   105
     */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   106
    public int getPort() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   107
        return port;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   108
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   109
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   110
    /**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   111
     * Shuts down the proxy, probably aborting any connections
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   112
     * currently open
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   113
     */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   114
    public void close() throws IOException {
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   115
        if (debug) System.out.println("Proxy: closing server");
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   116
        done = true;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   117
        listener.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   118
        for (Connection c : connections) {
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   119
            c.close();
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   120
            c.awaitCompletion();
36131
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
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   123
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   124
    final CopyOnWriteArrayList<Connection> connections;
36131
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
    volatile boolean done;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   127
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   128
    public void run() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   129
        if (System.getSecurityManager() == null) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   130
            execute();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   131
        } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   132
            // so calling domain does not need to have socket permission
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   133
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   134
                public Void run() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   135
                    execute();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   136
                    return null;
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
            });
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   139
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   140
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   141
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   142
    public void execute() {
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   143
        int id = 0;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   144
        try {
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   145
            while (!done) {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   146
                Socket s = listener.accept();
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   147
                id++;
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   148
                Connection c = new Connection(s, id);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   149
                if (debug)
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   150
                    System.out.println("Proxy: accepted new connection: " + s);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   151
                connections.add(c);
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   152
                c.init();
36131
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
        } catch(Throwable e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   155
            if (debug && !done) {
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   156
                System.out.println("Proxy: Fatal error, listener got " + e);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   157
                e.printStackTrace();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   158
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   159
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   160
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   161
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   162
    /**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   163
     * Transparently forward everything, once we know what the destination is
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   164
     */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   165
    class Connection {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   166
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   167
        private final int id;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   168
        Socket clientSocket, serverSocket;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   169
        Thread out, in;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   170
        volatile InputStream clientIn, serverIn;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   171
        volatile OutputStream clientOut, serverOut;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   172
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   173
        final static int CR = 13;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   174
        final static int LF = 10;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   175
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   176
        Connection(Socket s, int id) throws IOException {
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   177
            this.id = id;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   178
            this.clientSocket= s;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   179
            this.clientIn = new BufferedInputStream(s.getInputStream());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   180
            this.clientOut = s.getOutputStream();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   181
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   182
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   183
        byte[] readHeaders(InputStream is) throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   184
            byte[] outbuffer = new byte[8000];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   185
            int crlfcount = 0;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   186
            int bytecount = 0;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   187
            int c;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   188
            while ((c=is.read()) != -1 && bytecount < outbuffer.length) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   189
                outbuffer[bytecount++] = (byte)c;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   190
                if (debug) System.out.write(c);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   191
                // were looking for CRLFCRLF sequence
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   192
                if (c == CR || c == LF) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   193
                    switch(crlfcount) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   194
                        case 0:
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   195
                            if (c == CR) crlfcount ++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   196
                            break;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   197
                        case 1:
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   198
                            if (c == LF) crlfcount ++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   199
                            break;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   200
                        case 2:
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   201
                            if (c == CR) crlfcount ++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   202
                            break;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   203
                        case 3:
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   204
                            if (c == LF) crlfcount ++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   205
                            break;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   206
                    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   207
                } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   208
                    crlfcount = 0;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   209
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   210
                if (crlfcount == 4) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   211
                    break;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   212
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   213
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   214
            byte[] ret = new byte[bytecount];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   215
            System.arraycopy(outbuffer, 0, ret, 0, bytecount);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   216
            return ret;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   217
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   218
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   219
        boolean running() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   220
            return out.isAlive() || in.isAlive();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   221
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   222
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   223
        private volatile boolean closing;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   224
        public synchronized void close() throws IOException {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   225
            closing = true;
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   226
            if (debug)
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   227
                System.out.println("Proxy: closing connection {" + this + "}");
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   228
            if (serverSocket != null)
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   229
                serverSocket.close();
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   230
            if (clientSocket != null)
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   231
                clientSocket.close();
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   232
        }
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   233
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   234
        public void awaitCompletion() {
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   235
            try {
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   236
                if (in != null)
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   237
                    in.join();
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   238
                if (out!= null)
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   239
                    out.join();
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   240
            } catch (InterruptedException e) { }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   241
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   242
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   243
        int findCRLF(byte[] b) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   244
            for (int i=0; i<b.length-1; i++) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   245
                if (b[i] == CR && b[i+1] == LF) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   246
                    return i;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   247
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   248
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   249
            return -1;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   250
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   251
53521
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   252
        // Checks credentials in the request against those allowable by the proxy.
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   253
        private boolean authorized(Credentials credentials,
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   254
                                   List<String> requestHeaders) {
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   255
            List<String> authorization = requestHeaders.stream()
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   256
                    .filter(n -> n.toLowerCase(Locale.US).startsWith("proxy-authorization"))
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   257
                    .collect(toList());
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   258
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   259
            if (authorization.isEmpty())
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   260
                return false;
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   261
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   262
            if (authorization.size() != 1) {
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   263
                throw new IllegalStateException("Authorization unexpected count:" + authorization);
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   264
            }
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   265
            String value = authorization.get(0).substring("proxy-authorization".length()).trim();
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   266
            if (!value.startsWith(":"))
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   267
                throw new IllegalStateException("Authorization malformed: " + value);
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   268
            value = value.substring(1).trim();
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   269
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   270
            if (!value.startsWith("Basic "))
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   271
                throw new IllegalStateException("Authorization not Basic: " + value);
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   272
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   273
            value = value.substring("Basic ".length());
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   274
            String values = new String(Base64.getDecoder().decode(value), UTF_8);
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   275
            int sep = values.indexOf(':');
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   276
            if (sep < 1) {
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   277
                throw new IllegalStateException("Authorization no colon: " +  values);
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   278
            }
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   279
            String name = values.substring(0, sep);
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   280
            String password = values.substring(sep + 1);
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   281
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   282
            if (name.equals(credentials.name()) && password.equals(credentials.password()))
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   283
                return true;
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   284
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   285
            return false;
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   286
        }
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   287
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   288
        public void init() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   289
            try {
53521
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   290
                byte[] buf;
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   291
                while (true) {
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   292
                    buf = readHeaders(clientIn);
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   293
                    if (findCRLF(buf) == -1) {
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   294
                        if (debug)
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   295
                            System.out.println("Proxy: no CRLF closing, buf contains:["
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   296
                                    + new String(buf, UTF_8) + "]" );
53521
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   297
                        close();
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   298
                        return;
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   299
                    }
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   300
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   301
                    List<String> headers = asList(new String(buf, UTF_8).split("\r\n"));
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   302
                    // check authorization credentials, if required by the server
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   303
                    if (credentials != null && !authorized(credentials, headers)) {
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   304
                        String resp = "HTTP/1.1 407 Proxy Authentication Required\r\n" +
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   305
                                      "Content-Length: 0\r\n" +
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   306
                                      "Proxy-Authenticate: Basic realm=\"proxy realm\"\r\n\r\n";
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   307
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   308
                        clientOut.write(resp.getBytes(UTF_8));
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   309
                    } else {
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   310
                        break;
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   311
                    }
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   312
                }
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   313
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   314
                int p = findCRLF(buf);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   315
                String cmd = new String(buf, 0, p, "US-ASCII");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   316
                String[] params = cmd.split(" ");
53521
41fa3e6f2785 8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents: 49944
diff changeset
   317
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   318
                if (params[0].equals("CONNECT")) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   319
                    doTunnel(params[1]);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   320
                } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   321
                    doProxy(params[1], buf, p, cmd);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   322
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   323
            } catch (Throwable e) {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   324
                if (debug) {
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   325
                    System.out.println("Proxy: " + e);
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   326
                    e.printStackTrace();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   327
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   328
                try {close(); } catch (IOException e1) {}
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   329
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   330
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   331
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   332
        void doProxy(String dest, byte[] buf, int p, String cmdLine)
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   333
            throws IOException
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   334
        {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   335
            try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   336
                URI uri = new URI(dest);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   337
                if (!uri.isAbsolute()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   338
                    throw new IOException("request URI not absolute");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   339
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   340
                dest = uri.getAuthority();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   341
                // now extract the path from the URI and recreate the cmd line
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   342
                int sp = cmdLine.indexOf(' ');
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   343
                String method = cmdLine.substring(0, sp);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   344
                cmdLine = method + " " + uri.getPath() + " HTTP/1.1";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   345
                int x = cmdLine.length() - 1;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   346
                int i = p;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   347
                while (x >=0) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   348
                    buf[i--] = (byte)cmdLine.charAt(x--);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   349
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   350
                i++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   351
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   352
                commonInit(dest, 80);
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   353
                OutputStream sout;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   354
                synchronized (this) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   355
                    if (closing) return;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   356
                    sout = serverOut;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   357
                }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   358
                // might fail if we're closing but we don't care.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   359
                sout.write(buf, i, buf.length-i);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   360
                proxyCommon();
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
            } catch (URISyntaxException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   363
                throw new IOException(e);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   364
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   365
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   366
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   367
        synchronized void commonInit(String dest, int defaultPort) throws IOException {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   368
            if (closing) return;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   369
            int port;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   370
            String[] hostport = dest.split(":");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   371
            if (hostport.length == 1) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   372
                port = defaultPort;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   373
            } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   374
                port = Integer.parseInt(hostport[1]);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   375
            }
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   376
            if (debug)
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   377
                System.out.printf("Proxy: connecting to (%s/%d)\n", hostport[0], port);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   378
            serverSocket = new Socket(hostport[0], port);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   379
            serverOut = serverSocket.getOutputStream();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   380
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   381
            serverIn = new BufferedInputStream(serverSocket.getInputStream());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   382
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   383
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   384
        synchronized void proxyCommon() throws IOException {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   385
            if (closing) return;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   386
            out = new Thread(() -> {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   387
                try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   388
                    byte[] bb = new byte[8000];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   389
                    int n;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   390
                    while ((n = clientIn.read(bb)) != -1) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   391
                        serverOut.write(bb, 0, n);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   392
                    }
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   393
                    closing = true;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   394
                    serverSocket.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   395
                    clientSocket.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   396
                } catch (IOException e) {
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   397
                    if (!closing && debug) {
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   398
                        System.out.println("Proxy: " + e);
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   399
                        e.printStackTrace();
36131
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
            });
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   403
            in = new Thread(() -> {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   404
                try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   405
                    byte[] bb = new byte[8000];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   406
                    int n;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   407
                    while ((n = serverIn.read(bb)) != -1) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   408
                        clientOut.write(bb, 0, n);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   409
                    }
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   410
                    closing = true;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   411
                    serverSocket.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   412
                    clientSocket.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   413
                } catch (IOException e) {
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   414
                    if (!closing && debug) {
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   415
                        System.out.println("Proxy: " + e);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   416
                        e.printStackTrace();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   417
                    }
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
            });
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   420
            out.setName("Proxy-outbound");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   421
            out.setDaemon(true);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   422
            in.setDaemon(true);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   423
            in.setName("Proxy-inbound");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   424
            out.start();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   425
            in.start();
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
        void doTunnel(String dest) throws IOException {
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   429
            if (closing) return; // no need to go further.
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   430
            commonInit(dest, 443);
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   431
            // might fail if we're closing, but we don't care.
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   432
            clientOut.write("HTTP/1.1 200 OK\r\n\r\n".getBytes());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   433
            proxyCommon();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   434
        }
53551
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   435
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   436
        @Override
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   437
        public String toString() {
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   438
            return "Proxy connection " + id + ", client sock:" + clientSocket;
ab949b3e39bc 8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents: 53521
diff changeset
   439
        }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   440
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   441
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   442
    public static void main(String[] args) throws Exception {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   443
        int port = Integer.parseInt(args[0]);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   444
        boolean debug = args.length > 1 && args[1].equals("-debug");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   445
        System.out.println("Debugging : " + debug);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   446
        ProxyServer ps = new ProxyServer(port, debug);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   447
        System.out.println("Proxy server listening on port " + ps.getPort());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   448
        while (true) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   449
            Thread.sleep(5000);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   450
        }
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
}