Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/bugs/FixedLengthInputStream.java
38867 views
/*1* Copyright (c) 2008, 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*/2829import java.io.InputStream;30import java.io.IOException;31import java.io.OutputStream;32import java.io.PrintStream;33import java.net.InetSocketAddress;34import java.net.HttpURLConnection;35import java.net.URL;36import java.net.Socket;37import java.util.logging.*;38import com.sun.net.httpserver.HttpExchange;39import com.sun.net.httpserver.HttpHandler;40import com.sun.net.httpserver.HttpServer;4142public class FixedLengthInputStream43{44static final long POST_SIZE = 4L * 1024L * 1024L * 1024L; // 4Gig4546void test(String[] args) throws IOException {47HttpServer httpServer = startHttpServer();48int port = httpServer.getAddress().getPort();49try {50URL url = new URL("http://localhost:" + port + "/flis/");51HttpURLConnection uc = (HttpURLConnection)url.openConnection();52uc.setDoOutput(true);53uc.setRequestMethod("POST");54uc.setFixedLengthStreamingMode(POST_SIZE);55OutputStream os = uc.getOutputStream();5657/* create a 32K byte array with data to POST */58int thirtyTwoK = 32 * 1024;59byte[] ba = new byte[thirtyTwoK];60for (int i =0; i<thirtyTwoK; i++)61ba[i] = (byte)i;6263long times = POST_SIZE / thirtyTwoK;64for (int i=0; i<times; i++) {65os.write(ba);66}6768os.close();69InputStream is = uc.getInputStream();70while(is.read(ba) != -1);71is.close();7273pass();74} finally {75httpServer.stop(0);76}77}7879/**80* Http Server81*/82HttpServer startHttpServer() throws IOException {83if (debug) {84Logger logger =85Logger.getLogger("com.sun.net.httpserver");86Handler outHandler = new StreamHandler(System.out,87new SimpleFormatter());88outHandler.setLevel(Level.FINEST);89logger.setLevel(Level.FINEST);90logger.addHandler(outHandler);91}92HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);93httpServer.createContext("/flis/", new MyHandler(POST_SIZE));94httpServer.start();95return httpServer;96}9798class MyHandler implements HttpHandler {99static final int BUFFER_SIZE = 32 * 1024;100long expected;101102MyHandler(long expected){103this.expected = expected;104}105106@Override107public void handle(HttpExchange t) throws IOException {108InputStream is = t.getRequestBody();109byte[] ba = new byte[BUFFER_SIZE];110int read;111long count = 0L;112while((read = is.read(ba)) != -1) {113count += read;114}115is.close();116117check(count == expected, "Expected: " + expected + ", received "118+ count);119120debug("Received " + count + " bytes");121122t.sendResponseHeaders(200, -1);123t.close();124}125}126127//--------------------- Infrastructure ---------------------------128boolean debug = true;129volatile int passed = 0, failed = 0;130void pass() {passed++;}131void fail() {failed++; Thread.dumpStack();}132void fail(String msg) {System.err.println(msg); fail();}133void unexpected(Throwable t) {failed++; t.printStackTrace();}134void check(boolean cond) {if (cond) pass(); else fail();}135void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}136void debug(String message) {if(debug) { System.out.println(message); } }137public static void main(String[] args) throws Throwable {138Class<?> k = new Object(){}.getClass().getEnclosingClass();139try {k.getMethod("instanceMain",String[].class)140.invoke( k.newInstance(), (Object) args);}141catch (Throwable e) {throw e.getCause();}}142public void instanceMain(String[] args) throws Throwable {143try {test(args);} catch (Throwable t) {unexpected(t);}144System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);145if (failed > 0) throw new AssertionError("Some tests failed");}146147}148149150