author | amurillo |
Wed, 15 Aug 2012 16:49:38 -0700 | |
changeset 13465 | d3fc5d192448 |
parent 10699 | c6b5b935cce7 |
child 14342 | 8435a30053c1 |
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 |
|
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
26 |
* @bug 4380568 7095949 |
2 | 27 |
* @summary HttpURLConnection does not support 307 redirects |
28 |
*/ |
|
29 |
import java.io.*; |
|
30 |
import java.net.*; |
|
31 |
||
32 |
class RedirServer extends Thread { |
|
33 |
||
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
34 |
static final int TIMEOUT = 10 * 1000; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
35 |
|
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
36 |
ServerSocket ss; |
2 | 37 |
int port; |
38 |
||
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
39 |
String reply1Part1 = "HTTP/1.1 307 Temporary Redirect\r\n" + |
2 | 40 |
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
41 |
"Server: Apache/1.3.14 (Unix)\r\n" + |
|
42 |
"Location: http://localhost:"; |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
43 |
String reply1Part2 = "/redirected.html\r\n" + |
2 | 44 |
"Connection: close\r\n" + |
45 |
"Content-Type: text/html; charset=iso-8859-1\r\n\r\n" + |
|
46 |
"<html>Hello</html>"; |
|
47 |
||
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
48 |
RedirServer (ServerSocket ss) throws IOException { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
49 |
this.ss = ss; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
50 |
this.ss.setSoTimeout(TIMEOUT); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
51 |
port = this.ss.getLocalPort(); |
2 | 52 |
} |
53 |
||
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
54 |
String reply2 = "HTTP/1.1 200 Ok\r\n" + |
2 | 55 |
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
56 |
"Server: Apache/1.3.14 (Unix)\r\n" + |
|
57 |
"Connection: close\r\n" + |
|
58 |
"Content-Type: text/html; charset=iso-8859-1\r\n\r\n" + |
|
59 |
"World"; |
|
60 |
||
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
61 |
static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' }; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
62 |
|
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
63 |
// Read until the end of a HTTP request |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
64 |
void readOneRequest(InputStream is) throws IOException { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
65 |
int requestEndCount = 0, r; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
66 |
while ((r = is.read()) != -1) { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
67 |
if (r == requestEnd[requestEndCount]) { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
68 |
requestEndCount++; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
69 |
if (requestEndCount == 4) { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
70 |
break; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
71 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
72 |
} else { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
73 |
requestEndCount = 0; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
74 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
75 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
76 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
77 |
|
2 | 78 |
public void run () { |
79 |
try { |
|
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
80 |
try (Socket s = ss.accept()) { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
81 |
s.setSoTimeout(TIMEOUT); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
82 |
readOneRequest(s.getInputStream()); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
83 |
String reply = reply1Part1 + port + reply1Part2; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
84 |
s.getOutputStream().write(reply.getBytes()); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
85 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
86 |
|
2 | 87 |
/* wait for redirected connection */ |
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
88 |
try (Socket s = ss.accept()) { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
89 |
s.setSoTimeout(TIMEOUT); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
90 |
readOneRequest(s.getInputStream()); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
91 |
s.getOutputStream().write(reply2.getBytes()); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
92 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
93 |
} catch (Exception e) { |
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
94 |
e.printStackTrace(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
95 |
} finally { |
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
96 |
try { ss.close(); } catch (IOException unused) {} |
2 | 97 |
} |
98 |
} |
|
99 |
}; |
|
100 |
||
101 |
public class Redirect307Test { |
|
102 |
public static void main(String[] args) throws Exception { |
|
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
103 |
ServerSocket sock = new ServerSocket(0); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
104 |
int port = sock.getLocalPort(); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
105 |
RedirServer server = new RedirServer(sock); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
106 |
server.start(); |
2 | 107 |
|
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
108 |
URL url = new URL("http://localhost:" + port); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
109 |
URLConnection conURL = url.openConnection(); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
110 |
conURL.setDoInput(true); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
111 |
conURL.setAllowUserInteraction(false); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
112 |
conURL.setUseCaches(false); |
2 | 113 |
|
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
114 |
try (InputStream in = conURL.getInputStream()) { |
2 | 115 |
if ((in.read() != (int)'W') || (in.read()!=(int)'o')) { |
116 |
throw new RuntimeException ("Unexpected string read"); |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
} |