Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/TruncatedRequestBody.java
41155 views
/*1* Copyright (c) 2017, 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 819079326* @summary Httpserver does not detect truncated request body27*/2829import com.sun.net.httpserver.HttpContext;30import com.sun.net.httpserver.HttpExchange;31import com.sun.net.httpserver.HttpHandler;32import com.sun.net.httpserver.HttpServer;33import java.io.IOException;34import java.io.InputStream;35import java.io.OutputStream;36import java.net.InetAddress;37import java.net.InetSocketAddress;38import java.net.Socket;39import java.nio.charset.StandardCharsets;40import java.util.concurrent.CountDownLatch;41import java.util.concurrent.ExecutorService;42import java.util.concurrent.Executors;43import java.util.logging.ConsoleHandler;44import java.util.logging.Level;45import java.util.logging.Logger;4647/*48* Send two POST requests to the server which are both trucated49* and socket closed. Server needs to detect this and throw an IOException50* in getRequestBody().read(). Two variants for fixed length and chunked.51*/52public class TruncatedRequestBody {53static volatile boolean error = false;5455static CountDownLatch latch = new CountDownLatch(2);5657static class Handler implements HttpHandler {5859@Override60public void handle(HttpExchange exch) throws IOException {61InputStream is = exch.getRequestBody();62int c, count = 0;63byte[] buf = new byte[128];64try {65while ((c=is.read(buf)) > 0)66count += c;67} catch (IOException e) {68System.out.println("Exception caught");69latch.countDown();70throw e;71}72// shouldn't get to here73error = true;74latch.countDown();75System.out.println("Read " + count + " bytes");76is.close();77exch.sendResponseHeaders(200, -1);78}7980}8182/**83* @param args the command line arguments84*/85public static void main(String[] args) throws IOException, InterruptedException {86Logger logger = Logger.getLogger("com.sun.net.httpserver");87ConsoleHandler h = new ConsoleHandler();88h.setLevel(Level.ALL);89logger.setLevel(Level.ALL);90logger.addHandler(h);9192InetAddress loopback = InetAddress.getLoopbackAddress();93InetSocketAddress addr = new InetSocketAddress(loopback, 0);94HttpServer server = HttpServer.create(addr, 10);95HttpContext ct = server.createContext("/", new Handler());96ExecutorService ex = Executors.newCachedThreadPool();97server.setExecutor(ex);98server.start();99100int port = server.getAddress().getPort();101102// Test 1: fixed length103104Socket sock = new Socket(loopback, port);105String s1 = "POST /foo HTTP/1.1\r\nContent-length: 200000\r\n"106+ "\r\nfoo bar99";107108OutputStream os = sock.getOutputStream();109os.write(s1.getBytes(StandardCharsets.ISO_8859_1));110Thread.sleep(500);111112sock.close();113114// Test 2: chunked115116String s2 = "POST /foo HTTP/1.1\r\nTransfer-encoding: chunked\r\n\r\n" +117"100\r\nFoo bar";118sock = new Socket(loopback, port);119os = sock.getOutputStream();120os.write(s2.getBytes(StandardCharsets.ISO_8859_1));121Thread.sleep(500);122sock.close();123latch.await();124server.stop(0);125ex.shutdownNow();126if (error)127throw new RuntimeException("Test failed");128}129}130131132