提交 bb244939 编写于 作者: C chegar

7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently

Reviewed-by: alanb
上级 b5955de7
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
/** /**
* @test * @test
* @bug 4380568 * @bug 4380568 7095949
* @summary HttpURLConnection does not support 307 redirects * @summary HttpURLConnection does not support 307 redirects
*/ */
import java.io.*; import java.io.*;
...@@ -31,10 +31,9 @@ import java.net.*; ...@@ -31,10 +31,9 @@ import java.net.*;
class RedirServer extends Thread { class RedirServer extends Thread {
ServerSocket s; static final int TIMEOUT = 10 * 1000;
Socket s1;
InputStream is; ServerSocket ss;
OutputStream os;
int port; int port;
String reply1Part1 = "HTTP/1.1 307 Temporary Redirect\r\n" + String reply1Part1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
...@@ -46,10 +45,10 @@ class RedirServer extends Thread { ...@@ -46,10 +45,10 @@ class RedirServer extends Thread {
"Content-Type: text/html; charset=iso-8859-1\r\n\r\n" + "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
"<html>Hello</html>"; "<html>Hello</html>";
RedirServer (ServerSocket y) { RedirServer (ServerSocket ss) throws IOException {
s = y; this.ss = ss;
port = s.getLocalPort(); this.ss.setSoTimeout(TIMEOUT);
System.out.println("Server created listening on " + port); port = this.ss.getLocalPort();
} }
String reply2 = "HTTP/1.1 200 Ok\r\n" + String reply2 = "HTTP/1.1 200 Ok\r\n" +
...@@ -59,74 +58,63 @@ class RedirServer extends Thread { ...@@ -59,74 +58,63 @@ class RedirServer extends Thread {
"Content-Type: text/html; charset=iso-8859-1\r\n\r\n" + "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
"World"; "World";
static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
// Read until the end of a HTTP request
void readOneRequest(InputStream is) throws IOException {
int requestEndCount = 0, r;
while ((r = is.read()) != -1) {
if (r == requestEnd[requestEndCount]) {
requestEndCount++;
if (requestEndCount == 4) {
break;
}
} else {
requestEndCount = 0;
}
}
}
public void run () { public void run () {
try { try {
s1 = s.accept (); try (Socket s = ss.accept()) {
is = s1.getInputStream (); s.setSoTimeout(TIMEOUT);
os = s1.getOutputStream (); readOneRequest(s.getInputStream());
is.read (); String reply = reply1Part1 + port + reply1Part2;
String reply = reply1Part1 + port + reply1Part2; s.getOutputStream().write(reply.getBytes());
os.write (reply.getBytes()); }
os.close();
/* wait for redirected connection */ /* wait for redirected connection */
s.setSoTimeout (5000); try (Socket s = ss.accept()) {
s1 = s.accept (); s.setSoTimeout(TIMEOUT);
is = s1.getInputStream (); readOneRequest(s.getInputStream());
os = s1.getOutputStream (); s.getOutputStream().write(reply2.getBytes());
is.read(); }
os.write (reply2.getBytes()); } catch (Exception e) {
os.close();
}
catch (Exception e) {
/* Just need thread to terminate */
System.out.println("Server: caught " + e);
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
try { s.close(); } catch (IOException unused) {} try { ss.close(); } catch (IOException unused) {}
} }
} }
}; };
public class Redirect307Test { public class Redirect307Test {
public static final int DELAY = 10;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
int port; ServerSocket sock = new ServerSocket(0);
RedirServer server; int port = sock.getLocalPort();
ServerSocket sock; RedirServer server = new RedirServer(sock);
server.start();
try {
sock = new ServerSocket (0); URL url = new URL("http://localhost:" + port);
port = sock.getLocalPort (); URLConnection conURL = url.openConnection();
} conURL.setDoInput(true);
catch (Exception e) { conURL.setAllowUserInteraction(false);
System.out.println ("Exception: " + e); conURL.setUseCaches(false);
return;
} try (InputStream in = conURL.getInputStream()) {
server = new RedirServer(sock);
server.start ();
try {
String s = "http://localhost:" + port;
URL url = new URL(s);
URLConnection conURL = url.openConnection();
conURL.setDoInput(true);
conURL.setAllowUserInteraction(false);
conURL.setUseCaches(false);
InputStream in = conURL.getInputStream();
if ((in.read() != (int)'W') || (in.read()!=(int)'o')) { if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
throw new RuntimeException ("Unexpected string read"); throw new RuntimeException ("Unexpected string read");
} }
} }
catch(IOException e) {
e.printStackTrace();
throw new RuntimeException ("Exception caught + " + e);
}
} }
} }
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
/** /**
* @test * @test
* @bug 4458085 * @bug 4458085 7095949
* @summary Redirects Limited to 5 * @summary Redirects Limited to 5
*/ */
...@@ -57,29 +57,43 @@ class RedirLimitServer extends Thread { ...@@ -57,29 +57,43 @@ class RedirLimitServer extends Thread {
final ServerSocket ss; final ServerSocket ss;
final int port; final int port;
RedirLimitServer(ServerSocket ss) { RedirLimitServer(ServerSocket ss) throws IOException {
this.ss = ss; this.ss = ss;
port = ss.getLocalPort(); port = this.ss.getLocalPort();
this.ss.setSoTimeout(TIMEOUT);
}
static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
// Read until the end of a HTTP request
void readOneRequest(InputStream is) throws IOException {
int requestEndCount = 0, r;
while ((r = is.read()) != -1) {
if (r == requestEnd[requestEndCount]) {
requestEndCount++;
if (requestEndCount == 4) {
break;
}
} else {
requestEndCount = 0;
}
}
} }
public void run() { public void run() {
try { try {
ss.setSoTimeout(TIMEOUT);
for (int i=0; i<NUM_REDIRECTS; i++) { for (int i=0; i<NUM_REDIRECTS; i++) {
try (Socket s = ss.accept()) { try (Socket s = ss.accept()) {
s.setSoTimeout(TIMEOUT); s.setSoTimeout(TIMEOUT);
InputStream is = s.getInputStream(); readOneRequest(s.getInputStream());
OutputStream os = s.getOutputStream();
is.read();
String reply = reply1 + port + "/redirect" + i + reply2; String reply = reply1 + port + "/redirect" + i + reply2;
os.write(reply.getBytes()); s.getOutputStream().write(reply.getBytes());
} }
} }
try (Socket s = ss.accept()) { try (Socket s = ss.accept()) {
InputStream is = s.getInputStream(); s.setSoTimeout(TIMEOUT);
OutputStream os = s.getOutputStream(); readOneRequest(s.getInputStream());
is.read(); s.getOutputStream().write(reply3.getBytes());
os.write(reply3.getBytes());
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -96,21 +110,17 @@ public class RedirectLimit { ...@@ -96,21 +110,17 @@ public class RedirectLimit {
RedirLimitServer server = new RedirLimitServer(ss); RedirLimitServer server = new RedirLimitServer(ss);
server.start(); server.start();
InputStream in = null; URL url = new URL("http://localhost:" + port);
try { URLConnection conURL = url.openConnection();
URL url = new URL("http://localhost:" + port);
URLConnection conURL = url.openConnection();
conURL.setDoInput(true); conURL.setDoInput(true);
conURL.setAllowUserInteraction(false); conURL.setAllowUserInteraction(false);
conURL.setUseCaches(false); conURL.setUseCaches(false);
in = conURL.getInputStream(); try (InputStream in = conURL.getInputStream()) {
if ((in.read() != (int)'W') || (in.read()!=(int)'o')) { if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
throw new RuntimeException("Unexpected string read"); throw new RuntimeException("Unexpected string read");
} }
} finally {
if ( in != null ) { in.close(); }
} }
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册