author | dfuchs |
Fri, 01 Feb 2019 14:24:40 +0000 | |
changeset 53604 | 635361ec5491 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 4620571 |
|
27 |
* @summary urlconnection following redirect uses protocol of original request |
|
28 |
*/ |
|
29 |
import java.io.*; |
|
30 |
import java.net.*; |
|
31 |
||
32 |
public class ProtocolRedirect { |
|
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 | 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 | 48 |
} |
49 |
} |
|
50 |
} |
|
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 | 58 |
|
59 |
// Send a header redirect to the peer telling it to go to the |
|
60 |
// https server on the host it sent the connection request to. |
|
61 |
private void sendReply() throws IOException { |
|
62 |
OutputStream out = sock.getOutputStream(); |
|
63 |
StringBuffer reply = new StringBuffer(); |
|
64 |
reply.append("HTTP/1.0 302 Found\r\n" |
|
65 |
+ "Location: https://" + sock.getLocalAddress().getHostAddress() |
|
66 |
+ "/\r\n\r\n"); |
|
67 |
out.write(reply.toString().getBytes()); |
|
68 |
} |
|
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 | 71 |
public void run() { |
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 | 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 | 95 |
} |
96 |
} |
|
97 |
||
98 |
} |