jdk/test/java/net/URL/PerConnectionProxy.java
author dfuchs
Tue, 28 Jul 2015 11:30:55 +0200
changeset 31929 1e2ee502d1be
parent 30820 0d4717a011d3
permissions -rw-r--r--
8132256: jaxp: Investigate removal of com/sun/org/apache/bcel/internal/util/ClassPath.java Summary: com/sun/org/apache/bcel/internal/util/ClassPath.java removed Reviewed-by: joehw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13788
diff changeset
     2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4920526
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Needs per connection proxy support for URLs
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 14342
diff changeset
    27
 * @modules java.base/sun.net.www
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @library ../../../sun/net/www/httptest/
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    29
 * @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback
4347
ab0a9f495844 6907177: Update jdk tests to remove unncessary -source and -target options
darcy
parents: 2
diff changeset
    30
 * @compile PerConnectionProxy.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 PerConnectionProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.net.www.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class PerConnectionProxy implements HttpCallback {
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    39
    static TestHttpServer server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public void request (HttpTransaction req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        req.setResponseEntityBody ("Hello .");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            req.sendResponse (200, "Ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        try {
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    52
            server = new TestHttpServer (new PerConnectionProxy(), 1, 10, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            ProxyServer pserver = new ProxyServer(InetAddress.getByName("localhost"), server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            // start proxy server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            new Thread(pserver).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            URL url = new URL("http://localhost:"+server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            // for non existing proxy expect an IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                InetSocketAddress isa = InetSocketAddress.createUnresolved("inexistent", 8080);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                Proxy proxy = new Proxy(Proxy.Type.HTTP, isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                HttpURLConnection urlc = (HttpURLConnection)url.openConnection (proxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                throw new RuntimeException("non existing per connection proxy should lead to IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            // for NO_PROXY, expect direct connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                HttpURLConnection urlc = (HttpURLConnection)url.openConnection (Proxy.NO_PROXY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                int respCode = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                urlc.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                throw new RuntimeException("direct connection should succeed :"+ioex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            // for a normal proxy setting expect to see connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            // goes through that proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                InetSocketAddress isa = InetSocketAddress.createUnresolved("localhost", pserver.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                Proxy p = new Proxy(Proxy.Type.HTTP, isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                HttpURLConnection urlc = (HttpURLConnection)url.openConnection (p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                int respCode = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                urlc.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                throw new RuntimeException("connection through a local proxy should succeed :"+ioex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (server != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static class ProxyServer extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        private static ServerSocket ss = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // client requesting for a tunnel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        private Socket clientSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * Origin server's address and port that the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         * wants to establish the tunnel for communication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        private InetAddress serverInetAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        private int     serverPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        public ProxyServer(InetAddress server, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            serverInetAddr = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            serverPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                clientSocket = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                processRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                System.out.println("Proxy Failed: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                catch (IOException excep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    System.out.println("ProxyServer close error: " + excep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    excep.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        private void processRequests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // connection set to the tunneling mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            Socket serverSocket = new Socket(serverInetAddr, serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            ProxyTunnel clientToServer = new ProxyTunnel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                                         clientSocket, serverSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            ProxyTunnel serverToClient = new ProxyTunnel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                                         serverSocket, clientSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            clientToServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            serverToClient.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            System.out.println("Proxy: Started tunneling.......");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            clientToServer.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            serverToClient.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            System.out.println("Proxy: Finished tunneling........");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            clientToServer.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            serverToClient.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
***************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
*                       helper methods follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
***************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         * This inner class provides unidirectional data flow through the sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         * by continuously copying bytes from the input socket onto the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         * socket, until both sockets are open and EOF has not been received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        static class ProxyTunnel extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            Socket sockIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            Socket sockOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            InputStream input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            OutputStream output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            public ProxyTunnel(Socket sockIn, Socket sockOut)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                this.sockIn = sockIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                this.sockOut = sockOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                input = sockIn.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                output = sockOut.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                int BUFFER_SIZE = 400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                byte[] buf = new byte[BUFFER_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                int count = 0;  // keep track of the amount of data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    while ((bytesRead = input.read(buf)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        output.write(buf, 0, bytesRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        count += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                     * The peer end has closed the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                     * we will close the tunnel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    if (!sockIn.isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        sockIn.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    if (!sockOut.isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        sockOut.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                } catch (IOException ignored) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
}