mirror of
https://github.com/openjdk/jdk.git
synced 2026-03-11 08:23:26 +00:00
7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
Reviewed-by: alanb
This commit is contained in:
parent
ebd4f912d7
commit
1d5bbbec86
@ -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;
|
||||
ServerSocket sock = new ServerSocket(0);
|
||||
int port = sock.getLocalPort();
|
||||
RedirServer server = new RedirServer(sock);
|
||||
server.start();
|
||||
|
||||
try {
|
||||
sock = new ServerSocket (0);
|
||||
port = sock.getLocalPort ();
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.out.println ("Exception: " + e);
|
||||
return;
|
||||
}
|
||||
URL url = new URL("http://localhost:" + port);
|
||||
URLConnection conURL = url.openConnection();
|
||||
conURL.setDoInput(true);
|
||||
conURL.setAllowUserInteraction(false);
|
||||
conURL.setUseCaches(false);
|
||||
|
||||
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();
|
||||
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(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user