Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6361557.java
41154 views
/*1* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 636155726* @run main/othervm B636155727* @summary Lightweight HTTP server quickly runs out of file descriptors on Linux28*/2930import com.sun.net.httpserver.*;3132import java.util.*;33import java.util.concurrent.*;34import java.io.*;35import java.nio.*;36import java.nio.channels.*;37import java.net.*;3839/**40* The test simply opens 1,000 separate connections41* and invokes one http request on each. The client does42* not close any sockets until after they are closed43* by the server. This verifies the basic ability44* of the server to manage a reasonable number of connections45*/46public class B6361557 {4748public static boolean error = false;49static final int NUM = 1000;5051static class Handler implements HttpHandler {52int invocation = 1;53public void handle (HttpExchange t)54throws IOException55{56InputStream is = t.getRequestBody();57Headers map = t.getRequestHeaders();58Headers rmap = t.getResponseHeaders();59while (is.read () != -1) ;60is.close();61t.sendResponseHeaders (200, -1);62t.close();63}64}6566final static String request = "GET /test/foo.html HTTP/1.1\r\nContent-length: 0\r\n\r\n";67final static ByteBuffer requestBuf = ByteBuffer.allocate(64).put(request.getBytes());6869public static void main (String[] args) throws Exception {70Handler handler = new Handler();71InetAddress loopback = InetAddress.getLoopbackAddress();72InetSocketAddress addr = new InetSocketAddress (loopback, 0);73HttpServer server = HttpServer.create (addr, 0);74HttpContext ctx = server.createContext ("/test", handler);7576ExecutorService executor = Executors.newCachedThreadPool();77server.setExecutor (executor);78server.start ();7980InetSocketAddress destaddr = new InetSocketAddress (81loopback, server.getAddress().getPort()82);83System.out.println ("destaddr " + destaddr);8485Selector selector = Selector.open ();86int requests = 0;87int responses = 0;88while (true) {89// we need to read responses from time to time: slightly90// increase the timeout with the amount of pending responses91// to give a chance to the server to reply.92int selres = selector.select (requests - responses + 1);93Set<SelectionKey> selkeys = selector.selectedKeys();94for (SelectionKey key : selkeys) {95if (key.isReadable()) {96SocketChannel chan = (SocketChannel)key.channel();97ByteBuffer buf = (ByteBuffer)key.attachment();98try {99int x = chan.read(buf);100if (x == -1 || responseComplete(buf)) {101System.out.print("_");102key.attach(null);103chan.close();104responses++;105}106} catch (IOException e) {107System.out.println(e);108}109}110}111if (requests < NUM) {112System.out.print(".");113SocketChannel schan = SocketChannel.open(destaddr);114requestBuf.rewind();115int c = 0;116while (requestBuf.remaining() > 0) {117c += schan.write(requestBuf);118}119schan.configureBlocking(false);120schan.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(100));121requests++;122}123if (responses == NUM) {124System.out.println ("Finished clients");125break;126}127}128server.stop (1);129selector.close();130executor.shutdown ();131132}133134/* Look for CR LF CR LF */135static boolean responseComplete(ByteBuffer buf) {136int pos = buf.position();137buf.flip();138byte[] lookingFor = new byte[] {'\r', '\n', '\r', '\n' };139int lookingForCount = 0;140while (buf.hasRemaining()) {141byte b = buf.get();142if (b == lookingFor[lookingForCount]) {143lookingForCount++;144if (lookingForCount == 4) {145return true;146}147} else {148lookingForCount = 0;149}150}151buf.position(pos);152buf.limit(buf.capacity());153return false;154}155}156157158