author | mikael |
Fri, 04 Dec 2015 15:08:49 -0800 | |
changeset 35090 | 1f5b6aa795d0 |
parent 10699 | c6b5b935cce7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
2 |
* Copyright (c) 2001, 2011, 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:
8547
diff
changeset
|
26 |
* @bug 4458085 7095949 |
2 | 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 { |
|
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
39 |
static final int TIMEOUT = 10 * 1000; |
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
40 |
static final int NUM_REDIRECTS = 9; |
2 | 41 |
|
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
42 |
static final String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" + |
2 | 43 |
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
44 |
"Server: Apache/1.3.14 (Unix)\r\n" + |
|
45 |
"Location: http://localhost:"; |
|
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
46 |
static final String reply2 = ".html\r\n" + |
2 | 47 |
"Connection: close\r\n" + |
48 |
"Content-Type: text/html; charset=iso-8859-1\r\n\r\n" + |
|
49 |
"<html>Hello</html>"; |
|
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
50 |
static final String reply3 = "HTTP/1.1 200 Ok\r\n" + |
2 | 51 |
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
52 |
"Server: Apache/1.3.14 (Unix)\r\n" + |
|
53 |
"Connection: close\r\n" + |
|
54 |
"Content-Type: text/html; charset=iso-8859-1\r\n\r\n" + |
|
55 |
"World"; |
|
56 |
||
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
57 |
final ServerSocket ss; |
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
58 |
final int port; |
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
59 |
|
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
60 |
RedirLimitServer(ServerSocket ss) throws IOException { |
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
61 |
this.ss = ss; |
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
62 |
port = this.ss.getLocalPort(); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
63 |
this.ss.setSoTimeout(TIMEOUT); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
64 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
65 |
|
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
66 |
static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' }; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
67 |
|
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
68 |
// Read until the end of a HTTP request |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
69 |
void readOneRequest(InputStream is) throws IOException { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
70 |
int requestEndCount = 0, r; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
71 |
while ((r = is.read()) != -1) { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
72 |
if (r == requestEnd[requestEndCount]) { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
73 |
requestEndCount++; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
74 |
if (requestEndCount == 4) { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
75 |
break; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
76 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
77 |
} else { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
78 |
requestEndCount = 0; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
79 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
80 |
} |
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
81 |
} |
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
82 |
|
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
83 |
public void run() { |
2 | 84 |
try { |
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
85 |
for (int i=0; i<NUM_REDIRECTS; i++) { |
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
86 |
try (Socket s = ss.accept()) { |
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
87 |
s.setSoTimeout(TIMEOUT); |
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
88 |
readOneRequest(s.getInputStream()); |
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
89 |
String reply = reply1 + port + "/redirect" + i + reply2; |
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
90 |
s.getOutputStream().write(reply.getBytes()); |
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
91 |
} |
2 | 92 |
} |
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
93 |
try (Socket s = ss.accept()) { |
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
94 |
s.setSoTimeout(TIMEOUT); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
95 |
readOneRequest(s.getInputStream()); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
96 |
s.getOutputStream().write(reply3.getBytes()); |
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
97 |
} |
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
98 |
} catch (Exception e) { |
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
99 |
e.printStackTrace(); |
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
100 |
} finally { |
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
101 |
try { ss.close(); } catch (IOException unused) {} |
2 | 102 |
} |
103 |
} |
|
104 |
}; |
|
105 |
||
106 |
public class RedirectLimit { |
|
107 |
public static void main(String[] args) throws Exception { |
|
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
108 |
ServerSocket ss = new ServerSocket (0); |
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
109 |
int port = ss.getLocalPort(); |
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
110 |
RedirLimitServer server = new RedirLimitServer(ss); |
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
111 |
server.start(); |
2 | 112 |
|
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
113 |
URL url = new URL("http://localhost:" + port); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
114 |
URLConnection conURL = url.openConnection(); |
2 | 115 |
|
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
116 |
conURL.setDoInput(true); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
117 |
conURL.setAllowUserInteraction(false); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
118 |
conURL.setUseCaches(false); |
2 | 119 |
|
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
8547
diff
changeset
|
120 |
try (InputStream in = conURL.getInputStream()) { |
2 | 121 |
if ((in.read() != (int)'W') || (in.read()!=(int)'o')) { |
8547
5a1f87621165
7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents:
7668
diff
changeset
|
122 |
throw new RuntimeException("Unexpected string read"); |
2 | 123 |
} |
124 |
} |
|
125 |
} |
|
126 |
} |