Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/FixedLengthInputStream.java
41154 views
/*1* Copyright (c) 2008, 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 6756771 675562526* @summary com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig27* @library /test/lib28* @run main FixedLengthInputStream29* @run main/othervm -Djava.net.preferIPv6Addresses=true FixedLengthInputStream30*/3132import java.io.InputStream;33import java.io.IOException;34import java.io.OutputStream;35import java.io.PrintStream;36import java.net.InetAddress;37import java.net.InetSocketAddress;38import java.net.HttpURLConnection;39import java.net.Proxy;40import java.net.URL;41import java.net.Socket;42import java.util.logging.*;43import com.sun.net.httpserver.HttpExchange;44import com.sun.net.httpserver.HttpHandler;45import com.sun.net.httpserver.HttpServer;46import jdk.test.lib.net.URIBuilder;4748public class FixedLengthInputStream49{50static final long POST_SIZE = 4L * 1024L * 1024L * 1024L; // 4Gig5152void test(String[] args) throws IOException {53HttpServer httpServer = startHttpServer();54int port = httpServer.getAddress().getPort();55try {56URL url = URIBuilder.newBuilder()57.scheme("http")58.loopback()59.port(port)60.path("/flis/")61.toURLUnchecked();62HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);63uc.setDoOutput(true);64uc.setRequestMethod("POST");65uc.setFixedLengthStreamingMode(POST_SIZE);66OutputStream os = uc.getOutputStream();6768/* create a 32K byte array with data to POST */69int thirtyTwoK = 32 * 1024;70byte[] ba = new byte[thirtyTwoK];71for (int i =0; i<thirtyTwoK; i++)72ba[i] = (byte)i;7374long times = POST_SIZE / thirtyTwoK;75for (int i=0; i<times; i++) {76os.write(ba);77}7879os.close();80InputStream is = uc.getInputStream();81while(is.read(ba) != -1);82is.close();8384pass();85} finally {86httpServer.stop(0);87}88}8990/**91* Http Server92*/93HttpServer startHttpServer() throws IOException {94if (debug) {95Logger logger =96Logger.getLogger("com.sun.net.httpserver");97Handler outHandler = new StreamHandler(System.out,98new SimpleFormatter());99outHandler.setLevel(Level.FINEST);100logger.setLevel(Level.FINEST);101logger.addHandler(outHandler);102}103InetAddress loopback = InetAddress.getLoopbackAddress();104HttpServer httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0);105httpServer.createContext("/flis/", new MyHandler(POST_SIZE));106httpServer.start();107return httpServer;108}109110class MyHandler implements HttpHandler {111static final int BUFFER_SIZE = 32 * 1024;112long expected;113114MyHandler(long expected){115this.expected = expected;116}117118@Override119public void handle(HttpExchange t) throws IOException {120InputStream is = t.getRequestBody();121byte[] ba = new byte[BUFFER_SIZE];122int read;123long count = 0L;124while((read = is.read(ba)) != -1) {125count += read;126}127is.close();128129check(count == expected, "Expected: " + expected + ", received "130+ count);131132debug("Received " + count + " bytes");133134t.sendResponseHeaders(200, -1);135t.close();136}137}138139//--------------------- Infrastructure ---------------------------140boolean debug = true;141volatile int passed = 0, failed = 0;142void pass() {passed++;}143void fail() {failed++; Thread.dumpStack();}144void fail(String msg) {System.err.println(msg); fail();}145void unexpected(Throwable t) {failed++; t.printStackTrace();}146void check(boolean cond) {if (cond) pass(); else fail();}147void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}148void debug(String message) {if(debug) { System.out.println(message); } }149public static void main(String[] args) throws Throwable {150Class<?> k = new Object(){}.getClass().getEnclosingClass();151try {k.getMethod("instanceMain",String[].class)152.invoke( k.newInstance(), (Object) args);}153catch (Throwable e) {throw e.getCause();}}154public void instanceMain(String[] args) throws Throwable {155try {test(args);} catch (Throwable t) {unexpected(t);}156System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);157if (failed > 0) throw new AssertionError("Some tests failed");}158159}160161162