Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/bugs/B6361557.java
38867 views
/*1* Copyright (c) 2006, 2010, 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();71InetSocketAddress addr = new InetSocketAddress (0);72HttpServer server = HttpServer.create (addr, 0);73HttpContext ctx = server.createContext ("/test", handler);7475ExecutorService executor = Executors.newCachedThreadPool();76server.setExecutor (executor);77server.start ();7879InetSocketAddress destaddr = new InetSocketAddress (80"127.0.0.1", server.getAddress().getPort()81);82System.out.println ("destaddr " + destaddr);8384Selector selector = Selector.open ();85int requests = 0;86int responses = 0;87while (true) {88int selres = selector.select (1);89Set<SelectionKey> selkeys = selector.selectedKeys();90for (SelectionKey key : selkeys) {91if (key.isReadable()) {92SocketChannel chan = (SocketChannel)key.channel();93ByteBuffer buf = (ByteBuffer)key.attachment();94try {95int x = chan.read(buf);96if (x == -1 || responseComplete(buf)) {97key.attach(null);98chan.close();99responses++;100}101} catch (IOException e) {}102}103}104if (requests < NUM) {105SocketChannel schan = SocketChannel.open(destaddr);106requestBuf.rewind();107int c = 0;108while (requestBuf.remaining() > 0) {109c += schan.write(requestBuf);110}111schan.configureBlocking(false);112schan.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(100));113requests++;114}115if (responses == NUM) {116System.out.println ("Finished clients");117break;118}119}120server.stop (1);121selector.close();122executor.shutdown ();123124}125126/* Look for CR LF CR LF */127static boolean responseComplete(ByteBuffer buf) {128int pos = buf.position();129buf.flip();130byte[] lookingFor = new byte[] {'\r', '\n', '\r', '\n' };131int lookingForCount = 0;132while (buf.hasRemaining()) {133byte b = buf.get();134if (b == lookingFor[lookingForCount]) {135lookingForCount++;136if (lookingForCount == 4) {137return true;138}139} else {140lookingForCount = 0;141}142}143buf.position(pos);144buf.limit(buf.capacity());145return false;146}147}148149150