Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/HttpsPost.java
38889 views
/*1* Copyright (c) 2001, 2011, 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 442307426* @summary Need to rebase all the duplicated classes from Merlin.27* This test will check out http POST28* @run main/othervm HttpsPost29*30* SunJSSE does not support dynamic system properties, no way to re-use31* system properties in samevm/agentvm mode.32*/3334import java.io.*;35import java.net.*;36import javax.net.ssl.*;3738public class HttpsPost {3940/*41* =============================================================42* Set the various variables needed for the tests, then43* specify what tests to run on each side.44*/4546/*47* Should we run the client or server in a separate thread?48* Both sides can throw exceptions, but do you have a preference49* as to which side should be the main thread.50*/51static boolean separateServerThread = true;5253/*54* Where do we find the keystores?55*/56static String pathToStores = "../../../../../../javax/net/ssl/etc";57static String keyStoreFile = "keystore";58static String trustStoreFile = "truststore";59static String passwd = "passphrase";6061/*62* Is the server ready to serve?63*/64volatile static boolean serverReady = false;6566/*67* Is the connection ready to close?68*/69volatile static boolean closeReady = false;7071/*72* Turn on SSL debugging?73*/74static boolean debug = false;7576/*77* Message posted78*/79static String postMsg = "Testing HTTP post on a https server";8081/*82* If the client or server is doing some kind of object creation83* that the other side depends on, and that thread prematurely84* exits, you may experience a hang. The test harness will85* terminate all hung threads after its timeout has expired,86* currently 3 minutes by default, but you might try to be87* smart about it....88*/8990/*91* Define the server side of the test.92*93* If the server prematurely exits, serverReady will be set to true94* to avoid infinite hangs.95*/96void doServerSide() throws Exception {97SSLServerSocketFactory sslssf =98(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();99SSLServerSocket sslServerSocket =100(SSLServerSocket) sslssf.createServerSocket(serverPort);101serverPort = sslServerSocket.getLocalPort();102103/*104* Signal Client, we're ready for his connect.105*/106serverReady = true;107108SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();109try {110InputStream sslIS = sslSocket.getInputStream();111OutputStream sslOS = sslSocket.getOutputStream();112BufferedReader br =113new BufferedReader(new InputStreamReader(sslIS));114PrintStream ps = new PrintStream(sslOS);115116// process HTTP POST request from client117System.out.println("status line: "+br.readLine());118String msg = null;119while ((msg = br.readLine()) != null && msg.length() > 0);120121msg = br.readLine();122if (msg.equals(postMsg)) {123ps.println("HTTP/1.1 200 OK\n\n");124} else {125ps.println("HTTP/1.1 500 Not OK\n\n");126}127ps.flush();128129// close the socket130while (!closeReady) {131Thread.sleep(50);132}133} finally {134sslSocket.close();135sslServerSocket.close();136}137}138139/*140* Define the client side of the test.141*142* If the server prematurely exits, serverReady will be set to true143* to avoid infinite hangs.144*/145void doClientSide() throws Exception {146HostnameVerifier reservedHV =147HttpsURLConnection.getDefaultHostnameVerifier();148try {149/*150* Wait for server to get started.151*/152while (!serverReady) {153Thread.sleep(50);154}155156// Send HTTP POST request to server157URL url = new URL("https://localhost:"+serverPort);158159HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());160HttpsURLConnection http = (HttpsURLConnection)url.openConnection();161http.setDoOutput(true);162163http.setRequestMethod("POST");164PrintStream ps = new PrintStream(http.getOutputStream());165try {166ps.println(postMsg);167ps.flush();168if (http.getResponseCode() != 200) {169throw new RuntimeException("test Failed");170}171} finally {172ps.close();173http.disconnect();174closeReady = true;175}176} finally {177HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);178}179}180181static class NameVerifier implements HostnameVerifier {182public boolean verify(String hostname, SSLSession session) {183return true;184}185}186187/*188* =============================================================189* The remainder is just support stuff190*/191192// use any free port by default193volatile int serverPort = 0;194195volatile Exception serverException = null;196volatile Exception clientException = null;197198public static void main(String[] args) throws Exception {199String keyFilename =200System.getProperty("test.src", "./") + "/" + pathToStores +201"/" + keyStoreFile;202String trustFilename =203System.getProperty("test.src", "./") + "/" + pathToStores +204"/" + trustStoreFile;205206System.setProperty("javax.net.ssl.keyStore", keyFilename);207System.setProperty("javax.net.ssl.keyStorePassword", passwd);208System.setProperty("javax.net.ssl.trustStore", trustFilename);209System.setProperty("javax.net.ssl.trustStorePassword", passwd);210211if (debug)212System.setProperty("javax.net.debug", "all");213214/*215* Start the tests.216*/217new HttpsPost();218}219220Thread clientThread = null;221Thread serverThread = null;222223/*224* Primary constructor, used to drive remainder of the test.225*226* Fork off the other side, then do your work.227*/228HttpsPost() throws Exception {229if (separateServerThread) {230startServer(true);231startClient(false);232} else {233startClient(true);234startServer(false);235}236237/*238* Wait for other side to close down.239*/240if (separateServerThread) {241serverThread.join();242} else {243clientThread.join();244}245246/*247* When we get here, the test is pretty much over.248*249* If the main thread excepted, that propagates back250* immediately. If the other thread threw an exception, we251* should report back.252*/253if (serverException != null)254throw serverException;255if (clientException != null)256throw clientException;257}258259void startServer(boolean newThread) throws Exception {260if (newThread) {261serverThread = new Thread() {262public void run() {263try {264doServerSide();265} catch (Exception e) {266/*267* Our server thread just died.268*269* Release the client, if not active already...270*/271System.err.println("Server died...");272serverReady = true;273serverException = e;274}275}276};277serverThread.start();278} else {279doServerSide();280}281}282283void startClient(boolean newThread) throws Exception {284if (newThread) {285clientThread = new Thread() {286public void run() {287try {288doClientSide();289} catch (Exception e) {290/*291* Our client thread just died.292*/293System.err.println("Client died...");294clientException = e;295}296}297};298clientThread.start();299} else {300doClientSide();301}302}303}304305306