2
|
1 |
/*
|
58678
|
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 4726087
|
30820
|
27 |
* @modules java.base/sun.net.www
|
2
|
28 |
* @library ../../httptest/
|
13788
|
29 |
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
|
2
|
30 |
* @run main RelativeRedirect
|
58678
|
31 |
* @run main/othervm -Djava.net.preferIPv6Addresses=true RelativeRedirect
|
2
|
32 |
* @summary URLConnection cannot handle redirects
|
|
33 |
*/
|
|
34 |
|
|
35 |
import java.io.*;
|
|
36 |
import java.net.*;
|
|
37 |
|
|
38 |
public class RelativeRedirect implements HttpCallback {
|
|
39 |
static int count = 0;
|
13788
|
40 |
static TestHttpServer server;
|
2
|
41 |
|
|
42 |
static class MyAuthenticator extends Authenticator {
|
|
43 |
public MyAuthenticator () {
|
|
44 |
super ();
|
|
45 |
}
|
|
46 |
|
|
47 |
public PasswordAuthentication getPasswordAuthentication ()
|
|
48 |
{
|
|
49 |
return (new PasswordAuthentication ("user", "Wrongpassword".toCharArray()));
|
|
50 |
}
|
|
51 |
}
|
|
52 |
|
|
53 |
void firstReply (HttpTransaction req) throws IOException {
|
|
54 |
req.addResponseHeader ("Connection", "close");
|
|
55 |
req.addResponseHeader ("Location", "/redirect/file.html");
|
|
56 |
req.sendResponse (302, "Moved Permamently");
|
|
57 |
req.orderlyClose();
|
|
58 |
}
|
|
59 |
|
|
60 |
void secondReply (HttpTransaction req) throws IOException {
|
|
61 |
if (req.getRequestURI().toString().equals("/redirect/file.html") &&
|
58678
|
62 |
req.getRequestHeader("Host").equals(authority(server.getLocalPort()))) {
|
2
|
63 |
req.setResponseEntityBody ("Hello .");
|
|
64 |
req.sendResponse (200, "Ok");
|
|
65 |
} else {
|
|
66 |
req.setResponseEntityBody (req.getRequestURI().toString());
|
|
67 |
req.sendResponse (400, "Bad request");
|
|
68 |
}
|
|
69 |
req.orderlyClose();
|
|
70 |
|
|
71 |
}
|
|
72 |
public void request (HttpTransaction req) {
|
|
73 |
try {
|
|
74 |
switch (count) {
|
|
75 |
case 0:
|
|
76 |
// server redirect to /redirect/file.html
|
|
77 |
firstReply (req);
|
|
78 |
break;
|
|
79 |
case 1:
|
|
80 |
// client retry to /redirect/file.html on same server
|
|
81 |
secondReply (req);
|
|
82 |
break;
|
|
83 |
}
|
|
84 |
count ++;
|
|
85 |
} catch (IOException e) {
|
|
86 |
e.printStackTrace();
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
58678
|
90 |
static String authority(int port) {
|
|
91 |
InetAddress loopback = InetAddress.getLoopbackAddress();
|
|
92 |
String hostaddr = loopback.getHostAddress();
|
|
93 |
if (hostaddr.indexOf(':') > -1) {
|
|
94 |
hostaddr = "[" + hostaddr + "]";
|
|
95 |
}
|
|
96 |
return hostaddr + ":" + port;
|
|
97 |
}
|
|
98 |
|
2
|
99 |
public static void main (String[] args) throws Exception {
|
58678
|
100 |
InetAddress loopback = InetAddress.getLoopbackAddress();
|
2
|
101 |
MyAuthenticator auth = new MyAuthenticator ();
|
|
102 |
Authenticator.setDefault (auth);
|
|
103 |
try {
|
58678
|
104 |
server = new TestHttpServer (new RelativeRedirect(), 1, 10, loopback, 0);
|
2
|
105 |
System.out.println ("Server: listening on port: " + server.getLocalPort());
|
58678
|
106 |
URL url = new URL("http://" + authority(server.getLocalPort()));
|
2
|
107 |
System.out.println ("client opening connection to: " + url);
|
58678
|
108 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (Proxy.NO_PROXY);
|
2
|
109 |
InputStream is = urlc.getInputStream ();
|
|
110 |
is.close();
|
|
111 |
} catch (Exception e) {
|
|
112 |
throw new RuntimeException(e);
|
|
113 |
} finally {
|
|
114 |
if (server != null) {
|
|
115 |
server.terminate();
|
|
116 |
}
|
|
117 |
}
|
|
118 |
}
|
|
119 |
}
|