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