author | jjiang |
Wed, 25 May 2016 03:06:24 +0100 | |
changeset 38557 | 5c485e1ea6fa |
parent 30820 | 0d4717a011d3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13788
diff
changeset
|
2 |
* Copyright (c) 2002, 2012, 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 |
31 |
* @summary URLConnection cannot handle redirects |
|
32 |
*/ |
|
33 |
||
34 |
import java.io.*; |
|
35 |
import java.net.*; |
|
36 |
||
37 |
public class RelativeRedirect implements HttpCallback { |
|
38 |
static int count = 0; |
|
13788 | 39 |
static TestHttpServer server; |
2 | 40 |
|
41 |
static class MyAuthenticator extends Authenticator { |
|
42 |
public MyAuthenticator () { |
|
43 |
super (); |
|
44 |
} |
|
45 |
||
46 |
public PasswordAuthentication getPasswordAuthentication () |
|
47 |
{ |
|
48 |
return (new PasswordAuthentication ("user", "Wrongpassword".toCharArray())); |
|
49 |
} |
|
50 |
} |
|
51 |
||
52 |
void firstReply (HttpTransaction req) throws IOException { |
|
53 |
req.addResponseHeader ("Connection", "close"); |
|
54 |
req.addResponseHeader ("Location", "/redirect/file.html"); |
|
55 |
req.sendResponse (302, "Moved Permamently"); |
|
56 |
req.orderlyClose(); |
|
57 |
} |
|
58 |
||
59 |
void secondReply (HttpTransaction req) throws IOException { |
|
60 |
if (req.getRequestURI().toString().equals("/redirect/file.html") && |
|
61 |
req.getRequestHeader("Host").equals("localhost:"+server.getLocalPort())) { |
|
62 |
req.setResponseEntityBody ("Hello ."); |
|
63 |
req.sendResponse (200, "Ok"); |
|
64 |
} else { |
|
65 |
req.setResponseEntityBody (req.getRequestURI().toString()); |
|
66 |
req.sendResponse (400, "Bad request"); |
|
67 |
} |
|
68 |
req.orderlyClose(); |
|
69 |
||
70 |
} |
|
71 |
public void request (HttpTransaction req) { |
|
72 |
try { |
|
73 |
switch (count) { |
|
74 |
case 0: |
|
75 |
// server redirect to /redirect/file.html |
|
76 |
firstReply (req); |
|
77 |
break; |
|
78 |
case 1: |
|
79 |
// client retry to /redirect/file.html on same server |
|
80 |
secondReply (req); |
|
81 |
break; |
|
82 |
} |
|
83 |
count ++; |
|
84 |
} catch (IOException e) { |
|
85 |
e.printStackTrace(); |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
public static void main (String[] args) throws Exception { |
|
90 |
MyAuthenticator auth = new MyAuthenticator (); |
|
91 |
Authenticator.setDefault (auth); |
|
92 |
try { |
|
13788 | 93 |
server = new TestHttpServer (new RelativeRedirect(), 1, 10, 0); |
2 | 94 |
System.out.println ("Server: listening on port: " + server.getLocalPort()); |
95 |
URL url = new URL("http://localhost:"+server.getLocalPort()); |
|
96 |
System.out.println ("client opening connection to: " + url); |
|
97 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
|
98 |
InputStream is = urlc.getInputStream (); |
|
99 |
is.close(); |
|
100 |
} catch (Exception e) { |
|
101 |
throw new RuntimeException(e); |
|
102 |
} finally { |
|
103 |
if (server != null) { |
|
104 |
server.terminate(); |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
} |