Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test8a.java
38855 views
/*1* Copyright (c) 2005, 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 627001526* @run main/othervm Test8a27* @summary Light weight HTTP server28*/2930import com.sun.net.httpserver.*;3132import java.util.concurrent.*;33import java.io.*;34import java.net.*;35import javax.net.ssl.*;3637/**38* Test POST large file via fixed len encoding39*/4041public class Test8a extends Test {4243public static void main (String[] args) throws Exception {44//Logger log = Logger.getLogger ("com.sun.net.httpserver");45//ConsoleHandler h = new ConsoleHandler();46//h.setLevel (Level.INFO);47//log.addHandler (h);48//log.setLevel (Level.INFO);49HttpsServer server = null;50ExecutorService executor = null;51try {52Handler handler = new Handler();53InetSocketAddress addr = new InetSocketAddress (0);54server = HttpsServer.create (addr, 0);55HttpContext ctx = server.createContext ("/test", handler);56executor = Executors.newCachedThreadPool();57SSLContext ssl = new SimpleSSLContext(System.getProperty("test.src")).get();58server.setHttpsConfigurator(new HttpsConfigurator (ssl));59server.setExecutor (executor);60server.start ();6162URL url = new URL ("https://localhost:"+server.getAddress().getPort()+"/test/foo.html");63System.out.print ("Test8a: " );64HttpsURLConnection urlc = (HttpsURLConnection)url.openConnection ();65urlc.setDoOutput (true);66urlc.setRequestMethod ("POST");67urlc.setHostnameVerifier (new DummyVerifier());68urlc.setSSLSocketFactory (ssl.getSocketFactory());69OutputStream os = new BufferedOutputStream (urlc.getOutputStream(), 8000);70for (int i=0; i<SIZE; i++) {71os.write (i % 250);72}73os.close();74int resp = urlc.getResponseCode();75if (resp != 200) {76throw new RuntimeException ("test failed response code");77}78InputStream is = urlc.getInputStream ();79for (int i=0; i<SIZE; i++) {80int f = is.read();81if (f != (i % 250)) {82System.out.println ("Setting error(" +f +")("+i+")" );83error = true;84break;85}86}87is.close();88} finally {89delay();90if (server != null) server.stop(2);91if (executor != null) executor.shutdown();92}93if (error) {94throw new RuntimeException ("test failed error");95}96System.out.println ("OK");9798}99100public static boolean error = false;101//final static int SIZE = 999999;102final static int SIZE = 9999;103104static class Handler implements HttpHandler {105int invocation = 1;106public void handle (HttpExchange t)107throws IOException108{109System.out.println ("Handler.handle");110InputStream is = t.getRequestBody();111Headers map = t.getRequestHeaders();112Headers rmap = t.getResponseHeaders();113int c, count=0;114while ((c=is.read ()) != -1) {115if (c != (count % 250)) {116System.out.println ("Setting error 1");117error = true;118break;119}120count ++;121}122if (count != SIZE) {123System.out.println ("Setting error 2");124error = true;125}126is.close();127t.sendResponseHeaders (200, SIZE);128System.out.println ("Sending 200 OK");129OutputStream os = new BufferedOutputStream(t.getResponseBody(), 8000);130for (int i=0; i<SIZE; i++) {131os.write (i % 250);132}133os.close();134System.out.println ("Finished");135}136}137}138139140