test/jdk/sun/net/www/protocol/http/ProtocolRedirect.java
author dfuchs
Fri, 01 Feb 2019 14:24:40 +0000
changeset 53604 635361ec5491
parent 47216 71c04702a3d5
permissions -rw-r--r--
8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException" Summary: changed the test to use the loopback interface. Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53604
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2002, 2019, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4620571
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary  urlconnection following redirect uses protocol of original request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class ProtocolRedirect {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    public static void main(String [] args) throws Exception {
53604
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    34
        try (Redirect server = new Redirect(new ServerSocket())) {
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    35
            ServerSocket ss = server.ssock;
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    36
            ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    37
            new Thread(server).start();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
53604
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    39
            URL url = new URL("http", ss.getInetAddress().getHostAddress(), ss.getLocalPort(), "/");
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    40
            String page = url.toString();
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    41
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    42
            conn.connect();
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    43
            if (conn.getResponseCode() != 302) {
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    44
                System.err.println("Bad response code received from: " + page);
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    45
                throw new RuntimeException("Test failed. Should get RespCode: 302. Got:" + conn.getResponseCode());
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    46
            }
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    47
            System.out.println("Received expected response code from: " + page);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
53604
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    52
class Redirect implements Runnable, Closeable {
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    53
    final ServerSocket ssock;
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    54
    volatile boolean stopped;
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    55
    Redirect(ServerSocket ss) {
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    56
        ssock = ss;
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    57
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // Send a header redirect to the peer telling it to go to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // https server on the host it sent the connection request to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private void sendReply() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        OutputStream out = sock.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        StringBuffer reply = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        reply.append("HTTP/1.0 302 Found\r\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                     + "Location: https://" + sock.getLocalAddress().getHostAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                     + "/\r\n\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        out.write(reply.toString().getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
53604
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    70
    volatile Socket sock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        try {
53604
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    73
            Socket s = sock = ssock.accept();
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    74
            s.setTcpNoDelay(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            sendReply();
53604
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    76
            s.shutdownOutput();
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    77
        } catch(Throwable t) {
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    78
            if (!stopped) {
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    79
                t.printStackTrace();
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    80
                throw new RuntimeException(String.valueOf(t), t);
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    81
            }
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    82
        }
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    83
    }
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    84
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    85
    public void close() {
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    86
        Socket s = sock;
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    87
        boolean done = stopped;
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    88
        if (done) return;
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    89
        stopped = true;
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    90
        try {
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    91
            if (s != null) s.close();
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    92
        } catch (Throwable x) {
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    93
        } finally {
635361ec5491 8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
dfuchs
parents: 47216
diff changeset
    94
            try { ssock.close(); } catch (Throwable x) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
}