2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2002, 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 {
|
|
34 |
int localPort;
|
|
35 |
new Thread(new Redirect()).start();
|
|
36 |
while ((localPort = Redirect.listenPort) == -1) {
|
|
37 |
Thread.sleep(1000);
|
|
38 |
}
|
|
39 |
|
|
40 |
String page = "http://localhost:"+localPort+"/";
|
|
41 |
URL url = new URL(page);
|
|
42 |
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
|
43 |
conn.connect();
|
|
44 |
if (conn.getResponseCode() != 302) {
|
|
45 |
throw new RuntimeException("Test failed. Should get RespCode: 302. Got:"+conn.getResponseCode());
|
|
46 |
}
|
|
47 |
}
|
|
48 |
}
|
|
49 |
|
|
50 |
class Redirect implements Runnable {
|
|
51 |
public static int listenPort = -1; // port to listen for connections on
|
|
52 |
|
|
53 |
// Send a header redirect to the peer telling it to go to the
|
|
54 |
// https server on the host it sent the connection request to.
|
|
55 |
private void sendReply() throws IOException {
|
|
56 |
OutputStream out = sock.getOutputStream();
|
|
57 |
StringBuffer reply = new StringBuffer();
|
|
58 |
reply.append("HTTP/1.0 302 Found\r\n"
|
|
59 |
+ "Location: https://" + sock.getLocalAddress().getHostAddress()
|
|
60 |
+ "/\r\n\r\n");
|
|
61 |
out.write(reply.toString().getBytes());
|
|
62 |
}
|
|
63 |
|
|
64 |
Socket sock;
|
|
65 |
public void run() {
|
|
66 |
try {
|
|
67 |
ServerSocket ssock = new ServerSocket();
|
|
68 |
ssock.bind(null);
|
|
69 |
listenPort = ssock.getLocalPort();
|
|
70 |
sock = ssock.accept();
|
|
71 |
sock.setTcpNoDelay(true);
|
|
72 |
sendReply();
|
|
73 |
sock.shutdownOutput();
|
|
74 |
} catch(IOException io) {
|
|
75 |
throw new RuntimeException(io.getCause());
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
}
|