提交 879f7042 编写于 作者: M michaelm

6510892: com/sun/net/httpserver/bugs/B6361557.java fails

Reviewed-by: chegar
上级 71f4d04a
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
/** /**
* @test * @test
* @bug 6361557 * @bug 6361557
* @run main/othervm B6361557
* @summary Lightweight HTTP server quickly runs out of file descriptors on Linux * @summary Lightweight HTTP server quickly runs out of file descriptors on Linux
*/ */
...@@ -63,6 +64,9 @@ public class B6361557 { ...@@ -63,6 +64,9 @@ public class B6361557 {
} }
} }
final static String request = "GET /test/foo.html HTTP/1.1\r\nContent-length: 0\r\n\r\n";
final static ByteBuffer requestBuf = ByteBuffer.allocate(64).put(request.getBytes());
public static void main (String[] args) throws Exception { public static void main (String[] args) throws Exception {
Handler handler = new Handler(); Handler handler = new Handler();
InetSocketAddress addr = new InetSocketAddress (0); InetSocketAddress addr = new InetSocketAddress (0);
...@@ -73,48 +77,72 @@ public class B6361557 { ...@@ -73,48 +77,72 @@ public class B6361557 {
server.setExecutor (executor); server.setExecutor (executor);
server.start (); server.start ();
ByteBuffer buf = ByteBuffer.allocate (4096);
InetSocketAddress destaddr = new InetSocketAddress ( InetSocketAddress destaddr = new InetSocketAddress (
"127.0.0.1", server.getAddress().getPort() "127.0.0.1", server.getAddress().getPort()
); );
System.out.println ("destaddr " + destaddr); System.out.println ("destaddr " + destaddr);
Selector selector = Selector.open (); Selector selector = Selector.open ();
int i = 0; int requests = 0;
int responses = 0;
while (true) { while (true) {
i ++;
int selres = selector.select (1); int selres = selector.select (1);
Set<SelectionKey> selkeys = selector.selectedKeys(); Set<SelectionKey> selkeys = selector.selectedKeys();
for (SelectionKey key : selkeys) { for (SelectionKey key : selkeys) {
if (key.isReadable()) { if (key.isReadable()) {
SocketChannel chan = (SocketChannel)key.channel(); SocketChannel chan = (SocketChannel)key.channel();
buf.clear(); ByteBuffer buf = (ByteBuffer)key.attachment();
try { try {
int x = chan.read (buf); int x = chan.read(buf);
if (x == -1) { if (x == -1 || responseComplete(buf)) {
key.attach(null);
chan.close(); chan.close();
responses++;
} }
} catch (IOException e) {} } catch (IOException e) {}
} }
} }
if (i< NUM) { if (requests < NUM) {
SocketChannel schan = SocketChannel.open (destaddr); SocketChannel schan = SocketChannel.open(destaddr);
String cmd = "GET /test/foo.html HTTP/1.1\r\nContent-length: 0\r\n\r\n"; requestBuf.rewind();
buf.rewind ();
buf.put (cmd.getBytes());
buf.flip();
int c = 0; int c = 0;
while (buf.remaining() > 0) { while (requestBuf.remaining() > 0) {
c += schan.write (buf); c += schan.write(requestBuf);
} }
schan.configureBlocking (false); schan.configureBlocking(false);
schan.register (selector, SelectionKey.OP_READ, null); schan.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(100));
} else { requests++;
}
if (responses == NUM) {
System.out.println ("Finished clients"); System.out.println ("Finished clients");
server.stop (1); break;
executor.shutdown (); }
return; }
server.stop (1);
selector.close();
executor.shutdown ();
}
/* Look for CR LF CR LF */
static boolean responseComplete(ByteBuffer buf) {
int pos = buf.position();
buf.flip();
byte[] lookingFor = new byte[] {'\r', '\n', '\r', '\n' };
int lookingForCount = 0;
while (buf.hasRemaining()) {
byte b = buf.get();
if (b == lookingFor[lookingForCount]) {
lookingForCount++;
if (lookingForCount == 4) {
return true;
}
} else {
lookingForCount = 0;
} }
} }
buf.position(pos);
buf.limit(buf.capacity());
return false;
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册