提交 4d1e2a6e 编写于 作者: C chegar

7020136: java/net/URLConnection/RedirectLimit.java fails intermittently

Summary: Increase the socket timeout and clean up the test
Reviewed-by: alanb
上级 51ebdf2d
/* /*
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -36,105 +36,81 @@ import java.io.*; ...@@ -36,105 +36,81 @@ import java.io.*;
import java.net.*; import java.net.*;
class RedirLimitServer extends Thread { class RedirLimitServer extends Thread {
static final int TIMEOUT = 10 * 1000;
static final int NUM_REDIRECTS = 9;
ServerSocket s; static final String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
Socket s1;
InputStream is;
OutputStream os;
int port;
int nredirects = 9;
String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
"Server: Apache/1.3.14 (Unix)\r\n" + "Server: Apache/1.3.14 (Unix)\r\n" +
"Location: http://localhost:"; "Location: http://localhost:";
String reply2 = ".html\r\n" + static final String reply2 = ".html\r\n" +
"Connection: close\r\n" + "Connection: close\r\n" +
"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>";
static final String reply3 = "HTTP/1.1 200 Ok\r\n" +
RedirLimitServer (ServerSocket y) {
s = y;
port = s.getLocalPort();
}
String reply3 = "HTTP/1.1 200 Ok\r\n" +
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
"Server: Apache/1.3.14 (Unix)\r\n" + "Server: Apache/1.3.14 (Unix)\r\n" +
"Connection: close\r\n" + "Connection: close\r\n" +
"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";
public void run () { final ServerSocket ss;
final int port;
RedirLimitServer(ServerSocket ss) {
this.ss = ss;
port = ss.getLocalPort();
}
public void run() {
try { try {
s.setSoTimeout (2000); ss.setSoTimeout(TIMEOUT);
for (int i=0; i<nredirects; i++) { for (int i=0; i<NUM_REDIRECTS; i++) {
s1 = s.accept (); try (Socket s = ss.accept()) {
s1.setSoTimeout (2000); s.setSoTimeout(TIMEOUT);
is = s1.getInputStream (); InputStream is = s.getInputStream();
os = s1.getOutputStream (); OutputStream os = s.getOutputStream();
is.read (); is.read();
String reply = reply1 + port + "/redirect" + i + reply2; String reply = reply1 + port + "/redirect" + i + reply2;
os.write (reply.getBytes()); os.write(reply.getBytes());
os.close(); }
} }
s1 = s.accept (); try (Socket s = ss.accept()) {
is = s1.getInputStream (); InputStream is = s.getInputStream();
os = s1.getOutputStream (); OutputStream os = s.getOutputStream();
is.read (); is.read();
os.write (reply3.getBytes()); os.write(reply3.getBytes());
os.close(); }
} } catch (Exception e) {
catch (Exception e) { e.printStackTrace();
/* Just need thread to terminate */
} finally { } finally {
try { s.close(); } catch (IOException unused) {} try { ss.close(); } catch (IOException unused) {}
} }
} }
}; };
public class RedirectLimit { public class RedirectLimit {
public static final int DELAY = 10;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
int nLoops = 1; ServerSocket ss = new ServerSocket (0);
int nSize = 10; int port = ss.getLocalPort();
int port, n =0; RedirLimitServer server = new RedirLimitServer(ss);
byte b[] = new byte[nSize]; server.start();
RedirLimitServer server;
ServerSocket sock;
InputStream in = null;
try { try {
sock = new ServerSocket (0); URL url = new URL("http://localhost:" + port);
port = sock.getLocalPort ();
}
catch (Exception e) {
System.out.println ("Exception: " + e);
return;
}
server = new RedirLimitServer(sock);
server.start ();
try {
String s = "http://localhost:" + port;
URL url = new URL(s);
URLConnection conURL = url.openConnection(); URLConnection conURL = url.openConnection();
conURL.setDoInput(true); conURL.setDoInput(true);
conURL.setAllowUserInteraction(false); conURL.setAllowUserInteraction(false);
conURL.setUseCaches(false); conURL.setUseCaches(false);
InputStream in = conURL.getInputStream(); 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 {
catch(IOException e) { if ( in != null ) { in.close(); }
throw new RuntimeException ("Exception caught " + e);
} }
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册