687N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
687N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
687N/A *
687N/A * This code is free software; you can redistribute it and/or modify it
687N/A * under the terms of the GNU General Public License version 2 only, as
687N/A * published by the Free Software Foundation.
687N/A *
687N/A * This code is distributed in the hope that it will be useful, but WITHOUT
687N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
687N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
687N/A * version 2 for more details (a copy is included in the LICENSE file that
687N/A * accompanied this code).
687N/A *
687N/A * You should have received a copy of the GNU General Public License version
687N/A * 2 along with this work; if not, write to the Free Software Foundation,
687N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
687N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
687N/A */
687N/A
687N/A/**
687N/A * @test
705N/A * @bug 6756771 6755625
687N/A * @summary com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
687N/A */
687N/A
687N/Aimport java.io.InputStream;
687N/Aimport java.io.IOException;
687N/Aimport java.io.OutputStream;
687N/Aimport java.io.PrintStream;
687N/Aimport java.net.InetSocketAddress;
687N/Aimport java.net.HttpURLConnection;
687N/Aimport java.net.URL;
687N/Aimport java.net.Socket;
687N/Aimport java.util.logging.*;
687N/Aimport com.sun.net.httpserver.HttpExchange;
687N/Aimport com.sun.net.httpserver.HttpHandler;
687N/Aimport com.sun.net.httpserver.HttpServer;
687N/A
687N/Apublic class FixedLengthInputStream
687N/A{
687N/A static final long POST_SIZE = 4L * 1024L * 1024L * 1024L; // 4Gig
687N/A
687N/A void test(String[] args) throws IOException {
687N/A HttpServer httpServer = startHttpServer();
687N/A int port = httpServer.getAddress().getPort();
687N/A try {
687N/A URL url = new URL("http://localhost:" + port + "/flis/");
687N/A HttpURLConnection uc = (HttpURLConnection)url.openConnection();
687N/A uc.setDoOutput(true);
687N/A uc.setRequestMethod("POST");
687N/A uc.setFixedLengthStreamingMode(POST_SIZE);
687N/A OutputStream os = uc.getOutputStream();
687N/A
687N/A /* create a 32K byte array with data to POST */
687N/A int thirtyTwoK = 32 * 1024;
687N/A byte[] ba = new byte[thirtyTwoK];
687N/A for (int i =0; i<thirtyTwoK; i++)
687N/A ba[i] = (byte)i;
687N/A
687N/A long times = POST_SIZE / thirtyTwoK;
687N/A for (int i=0; i<times; i++) {
687N/A os.write(ba);
687N/A }
687N/A
687N/A os.close();
687N/A InputStream is = uc.getInputStream();
687N/A while(is.read(ba) != -1);
687N/A is.close();
687N/A
705N/A pass();
687N/A } finally {
687N/A httpServer.stop(0);
687N/A }
687N/A }
687N/A
687N/A /**
687N/A * Http Server
687N/A */
687N/A HttpServer startHttpServer() throws IOException {
687N/A if (debug) {
687N/A Logger logger =
687N/A Logger.getLogger("com.sun.net.httpserver");
687N/A Handler outHandler = new StreamHandler(System.out,
687N/A new SimpleFormatter());
687N/A outHandler.setLevel(Level.FINEST);
687N/A logger.setLevel(Level.FINEST);
687N/A logger.addHandler(outHandler);
687N/A }
687N/A HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
687N/A httpServer.createContext("/flis/", new MyHandler(POST_SIZE));
687N/A httpServer.start();
687N/A return httpServer;
687N/A }
687N/A
687N/A class MyHandler implements HttpHandler {
687N/A static final int BUFFER_SIZE = 32 * 1024;
687N/A long expected;
687N/A
687N/A MyHandler(long expected){
687N/A this.expected = expected;
687N/A }
687N/A
687N/A @Override
687N/A public void handle(HttpExchange t) throws IOException {
687N/A InputStream is = t.getRequestBody();
687N/A byte[] ba = new byte[BUFFER_SIZE];
687N/A int read;
687N/A long count = 0L;
687N/A while((read = is.read(ba)) != -1) {
687N/A count += read;
687N/A }
687N/A is.close();
687N/A
687N/A check(count == expected, "Expected: " + expected + ", received "
687N/A + count);
687N/A
687N/A debug("Received " + count + " bytes");
687N/A
687N/A t.sendResponseHeaders(200, -1);
687N/A t.close();
687N/A }
687N/A }
687N/A
687N/A //--------------------- Infrastructure ---------------------------
687N/A boolean debug = true;
687N/A volatile int passed = 0, failed = 0;
687N/A void pass() {passed++;}
687N/A void fail() {failed++; Thread.dumpStack();}
687N/A void fail(String msg) {System.err.println(msg); fail();}
687N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
687N/A void check(boolean cond) {if (cond) pass(); else fail();}
687N/A void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
687N/A void debug(String message) {if(debug) { System.out.println(message); } }
687N/A public static void main(String[] args) throws Throwable {
687N/A Class<?> k = new Object(){}.getClass().getEnclosingClass();
687N/A try {k.getMethod("instanceMain",String[].class)
687N/A .invoke( k.newInstance(), (Object) args);}
687N/A catch (Throwable e) {throw e.getCause();}}
687N/A public void instanceMain(String[] args) throws Throwable {
687N/A try {test(args);} catch (Throwable t) {unexpected(t);}
687N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
687N/A if (failed > 0) throw new AssertionError("Some tests failed");}
687N/A
687N/A}