0N/A/*
3261N/A * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6361557
2629N/A * @run main/othervm B6361557
0N/A * @summary Lightweight HTTP server quickly runs out of file descriptors on Linux
0N/A */
0N/A
0N/Aimport com.sun.net.httpserver.*;
0N/A
0N/Aimport java.util.*;
0N/Aimport java.util.concurrent.*;
0N/Aimport java.io.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.channels.*;
0N/Aimport java.net.*;
0N/A
0N/A/**
2612N/A * The test simply opens 1,000 separate connections
0N/A * and invokes one http request on each. The client does
0N/A * not close any sockets until after they are closed
0N/A * by the server. This verifies the basic ability
0N/A * of the server to manage a reasonable number of connections
0N/A */
0N/Apublic class B6361557 {
0N/A
0N/A public static boolean error = false;
2612N/A static final int NUM = 1000;
0N/A
0N/A static class Handler implements HttpHandler {
0N/A int invocation = 1;
0N/A public void handle (HttpExchange t)
0N/A throws IOException
0N/A {
0N/A InputStream is = t.getRequestBody();
0N/A Headers map = t.getRequestHeaders();
0N/A Headers rmap = t.getResponseHeaders();
0N/A while (is.read () != -1) ;
0N/A is.close();
0N/A t.sendResponseHeaders (200, -1);
0N/A t.close();
0N/A }
0N/A }
0N/A
2629N/A final static String request = "GET /test/foo.html HTTP/1.1\r\nContent-length: 0\r\n\r\n";
2629N/A final static ByteBuffer requestBuf = ByteBuffer.allocate(64).put(request.getBytes());
2629N/A
0N/A public static void main (String[] args) throws Exception {
0N/A Handler handler = new Handler();
0N/A InetSocketAddress addr = new InetSocketAddress (0);
0N/A HttpServer server = HttpServer.create (addr, 0);
0N/A HttpContext ctx = server.createContext ("/test", handler);
0N/A
0N/A ExecutorService executor = Executors.newCachedThreadPool();
0N/A server.setExecutor (executor);
0N/A server.start ();
0N/A
0N/A InetSocketAddress destaddr = new InetSocketAddress (
0N/A "127.0.0.1", server.getAddress().getPort()
0N/A );
0N/A System.out.println ("destaddr " + destaddr);
0N/A
0N/A Selector selector = Selector.open ();
2629N/A int requests = 0;
2629N/A int responses = 0;
0N/A while (true) {
0N/A int selres = selector.select (1);
0N/A Set<SelectionKey> selkeys = selector.selectedKeys();
0N/A for (SelectionKey key : selkeys) {
0N/A if (key.isReadable()) {
0N/A SocketChannel chan = (SocketChannel)key.channel();
2629N/A ByteBuffer buf = (ByteBuffer)key.attachment();
0N/A try {
2629N/A int x = chan.read(buf);
2629N/A if (x == -1 || responseComplete(buf)) {
2629N/A key.attach(null);
0N/A chan.close();
2629N/A responses++;
0N/A }
0N/A } catch (IOException e) {}
0N/A }
0N/A }
2629N/A if (requests < NUM) {
2629N/A SocketChannel schan = SocketChannel.open(destaddr);
2629N/A requestBuf.rewind();
0N/A int c = 0;
2629N/A while (requestBuf.remaining() > 0) {
2629N/A c += schan.write(requestBuf);
0N/A }
2629N/A schan.configureBlocking(false);
2629N/A schan.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(100));
2629N/A requests++;
2629N/A }
2629N/A if (responses == NUM) {
0N/A System.out.println ("Finished clients");
2629N/A break;
0N/A }
0N/A }
2629N/A server.stop (1);
2629N/A selector.close();
2629N/A executor.shutdown ();
2629N/A
2629N/A }
2629N/A
2629N/A /* Look for CR LF CR LF */
2629N/A static boolean responseComplete(ByteBuffer buf) {
2629N/A int pos = buf.position();
2629N/A buf.flip();
2629N/A byte[] lookingFor = new byte[] {'\r', '\n', '\r', '\n' };
2629N/A int lookingForCount = 0;
2629N/A while (buf.hasRemaining()) {
2629N/A byte b = buf.get();
2629N/A if (b == lookingFor[lookingForCount]) {
2629N/A lookingForCount++;
2629N/A if (lookingForCount == 4) {
2629N/A return true;
2629N/A }
2629N/A } else {
2629N/A lookingForCount = 0;
2629N/A }
2629N/A }
2629N/A buf.position(pos);
2629N/A buf.limit(buf.capacity());
2629N/A return false;
0N/A }
0N/A}