提交 bb244939 编写于 作者: C chegar

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

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