author | chegar |
Fri, 27 Sep 2019 09:55:35 +0100 | |
changeset 58365 | 73950479184b |
parent 54841 | 43439afaab4a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54841
43439afaab4a
8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2001, 2019, 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 |
54841
43439afaab4a
8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents:
47216
diff
changeset
|
27 |
* @library /test/lib |
2 | 28 |
* @summary HttpURLConnection does not support 307 redirects |
29 |
*/ |
|
30 |
import java.io.*; |
|
31 |
import java.net.*; |
|
54841
43439afaab4a
8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents:
47216
diff
changeset
|
32 |
import jdk.test.lib.net.URIBuilder; |
58365
73950479184b
8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents:
54841
diff
changeset
|
33 |
import static java.net.Proxy.NO_PROXY; |
54841
43439afaab4a
8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents:
47216
diff
changeset
|
34 |
|
2 | 35 |
class RedirServer extends Thread { |
36 |
||
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
37 |
static final int TIMEOUT = 10 * 1000; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
38 |
|
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
39 |
ServerSocket ss; |
2 | 40 |
int port; |
41 |
||
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
42 |
String reply1Part1 = "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:"; |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
46 |
String reply1Part2 = "/redirected.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>"; |
|
50 |
||
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
51 |
RedirServer (ServerSocket ss) throws IOException { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
52 |
this.ss = ss; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
53 |
this.ss.setSoTimeout(TIMEOUT); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
54 |
port = this.ss.getLocalPort(); |
2 | 55 |
} |
56 |
||
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
57 |
String reply2 = "HTTP/1.1 200 Ok\r\n" + |
2 | 58 |
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
59 |
"Server: Apache/1.3.14 (Unix)\r\n" + |
|
60 |
"Connection: close\r\n" + |
|
61 |
"Content-Type: text/html; charset=iso-8859-1\r\n\r\n" + |
|
62 |
"World"; |
|
63 |
||
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
64 |
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
|
65 |
|
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
66 |
// Read until the end of a HTTP request |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
67 |
void readOneRequest(InputStream is) throws IOException { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
68 |
int requestEndCount = 0, r; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
69 |
while ((r = is.read()) != -1) { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
70 |
if (r == requestEnd[requestEndCount]) { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
71 |
requestEndCount++; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
72 |
if (requestEndCount == 4) { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
73 |
break; |
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 |
} else { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
76 |
requestEndCount = 0; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
77 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
78 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
79 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
80 |
|
2 | 81 |
public void run () { |
82 |
try { |
|
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
83 |
try (Socket s = ss.accept()) { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
84 |
s.setSoTimeout(TIMEOUT); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
85 |
readOneRequest(s.getInputStream()); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
86 |
String reply = reply1Part1 + port + reply1Part2; |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
87 |
s.getOutputStream().write(reply.getBytes()); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
88 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
89 |
|
2 | 90 |
/* wait for redirected connection */ |
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
91 |
try (Socket s = ss.accept()) { |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
92 |
s.setSoTimeout(TIMEOUT); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
93 |
readOneRequest(s.getInputStream()); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
94 |
s.getOutputStream().write(reply2.getBytes()); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
95 |
} |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
96 |
} catch (Exception e) { |
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
97 |
e.printStackTrace(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
98 |
} finally { |
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
99 |
try { ss.close(); } catch (IOException unused) {} |
2 | 100 |
} |
101 |
} |
|
102 |
}; |
|
103 |
||
104 |
public class Redirect307Test { |
|
105 |
public static void main(String[] args) throws Exception { |
|
54841
43439afaab4a
8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents:
47216
diff
changeset
|
106 |
ServerSocket sock = new ServerSocket(0, 0, InetAddress.getLoopbackAddress()); |
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
107 |
int port = sock.getLocalPort(); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
108 |
RedirServer server = new RedirServer(sock); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
109 |
server.start(); |
2 | 110 |
|
54841
43439afaab4a
8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents:
47216
diff
changeset
|
111 |
URL url = URIBuilder.newBuilder() |
43439afaab4a
8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents:
47216
diff
changeset
|
112 |
.scheme("http") |
43439afaab4a
8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents:
47216
diff
changeset
|
113 |
.loopback() |
43439afaab4a
8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents:
47216
diff
changeset
|
114 |
.port(port) |
43439afaab4a
8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents:
47216
diff
changeset
|
115 |
.toURL(); |
58365
73950479184b
8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents:
54841
diff
changeset
|
116 |
URLConnection conURL = url.openConnection(NO_PROXY); |
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
117 |
conURL.setDoInput(true); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
118 |
conURL.setAllowUserInteraction(false); |
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
119 |
conURL.setUseCaches(false); |
2 | 120 |
|
10699
c6b5b935cce7
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents:
7668
diff
changeset
|
121 |
try (InputStream in = conURL.getInputStream()) { |
2 | 122 |
if ((in.read() != (int)'W') || (in.read()!=(int)'o')) { |
123 |
throw new RuntimeException ("Unexpected string read"); |
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
127 |
} |