author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 6115 | 7c523cf2bc8a |
child 8547 | 5a1f87621165 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2001, 2010, 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 4458085 |
|
27 |
* @summary Redirects Limited to 5 |
|
28 |
*/ |
|
29 |
||
30 |
/* |
|
31 |
* Simulate a server that redirects ( to a different URL) 9 times |
|
32 |
* and see if the client correctly follows the trail |
|
33 |
*/ |
|
34 |
||
35 |
import java.io.*; |
|
36 |
import java.net.*; |
|
37 |
||
38 |
class RedirLimitServer extends Thread { |
|
39 |
||
40 |
ServerSocket s; |
|
41 |
Socket s1; |
|
42 |
InputStream is; |
|
43 |
OutputStream os; |
|
44 |
int port; |
|
45 |
int nredirects = 9; |
|
46 |
||
47 |
String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" + |
|
48 |
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
|
49 |
"Server: Apache/1.3.14 (Unix)\r\n" + |
|
50 |
"Location: http://localhost:"; |
|
51 |
String reply2 = ".html\r\n" + |
|
52 |
"Connection: close\r\n" + |
|
53 |
"Content-Type: text/html; charset=iso-8859-1\r\n\r\n" + |
|
54 |
"<html>Hello</html>"; |
|
55 |
||
56 |
RedirLimitServer (ServerSocket y) { |
|
57 |
s = y; |
|
58 |
port = s.getLocalPort(); |
|
59 |
} |
|
60 |
||
61 |
String reply3 = "HTTP/1.1 200 Ok\r\n" + |
|
62 |
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
|
63 |
"Server: Apache/1.3.14 (Unix)\r\n" + |
|
64 |
"Connection: close\r\n" + |
|
65 |
"Content-Type: text/html; charset=iso-8859-1\r\n\r\n" + |
|
66 |
"World"; |
|
67 |
||
68 |
public void run () { |
|
69 |
try { |
|
70 |
s.setSoTimeout (2000); |
|
71 |
for (int i=0; i<nredirects; i++) { |
|
72 |
s1 = s.accept (); |
|
73 |
s1.setSoTimeout (2000); |
|
74 |
is = s1.getInputStream (); |
|
75 |
os = s1.getOutputStream (); |
|
76 |
is.read (); |
|
77 |
String reply = reply1 + port + "/redirect" + i + reply2; |
|
78 |
os.write (reply.getBytes()); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
79 |
os.close(); |
2 | 80 |
} |
81 |
s1 = s.accept (); |
|
82 |
is = s1.getInputStream (); |
|
83 |
os = s1.getOutputStream (); |
|
84 |
is.read (); |
|
85 |
os.write (reply3.getBytes()); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
86 |
os.close(); |
2 | 87 |
} |
88 |
catch (Exception e) { |
|
89 |
/* Just need thread to terminate */ |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
90 |
} finally { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
91 |
try { s.close(); } catch (IOException unused) {} |
2 | 92 |
} |
93 |
} |
|
94 |
}; |
|
95 |
||
96 |
||
97 |
public class RedirectLimit { |
|
98 |
||
99 |
public static final int DELAY = 10; |
|
100 |
||
101 |
public static void main(String[] args) throws Exception { |
|
102 |
int nLoops = 1; |
|
103 |
int nSize = 10; |
|
104 |
int port, n =0; |
|
105 |
byte b[] = new byte[nSize]; |
|
106 |
RedirLimitServer server; |
|
107 |
ServerSocket sock; |
|
108 |
||
109 |
try { |
|
110 |
sock = new ServerSocket (0); |
|
111 |
port = sock.getLocalPort (); |
|
112 |
} |
|
113 |
catch (Exception e) { |
|
114 |
System.out.println ("Exception: " + e); |
|
115 |
return; |
|
116 |
} |
|
117 |
||
118 |
server = new RedirLimitServer(sock); |
|
119 |
server.start (); |
|
120 |
||
121 |
try { |
|
122 |
||
123 |
String s = "http://localhost:" + port; |
|
124 |
URL url = new URL(s); |
|
125 |
URLConnection conURL = url.openConnection(); |
|
126 |
||
127 |
conURL.setDoInput(true); |
|
128 |
conURL.setAllowUserInteraction(false); |
|
129 |
conURL.setUseCaches(false); |
|
130 |
||
131 |
InputStream in = conURL.getInputStream(); |
|
132 |
if ((in.read() != (int)'W') || (in.read()!=(int)'o')) { |
|
133 |
throw new RuntimeException ("Unexpected string read"); |
|
134 |
} |
|
135 |
} |
|
136 |
catch(IOException e) { |
|
137 |
throw new RuntimeException ("Exception caught " + e); |
|
138 |
} |
|
139 |
} |
|
140 |
} |