Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/RetryHttps.java
38889 views
/*1* Copyright (c) 2003, 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/* @test24* @bug 479942725* @summary Https can not retry request26* @run main/othervm RetryHttps27*28* SunJSSE does not support dynamic system properties, no way to re-use29* system properties in samevm/agentvm mode.30* @author Yingxian Wang31*/3233import java.net.*;34import java.util.*;35import java.io.*;36import javax.net.ssl.*;3738public class RetryHttps {39static Map cookies;40ServerSocket ss;4142/*43* =============================================================44* Set the various variables needed for the tests, then45* specify what tests to run on each side.46*/4748/*49* Should we run the client or server in a separate thread?50* Both sides can throw exceptions, but do you have a preference51* as to which side should be the main thread.52*/53static boolean separateServerThread = true;5455/*56* Where do we find the keystores?57*/58static String pathToStores = "../../../../../../javax/net/ssl/etc";59static String keyStoreFile = "keystore";60static String trustStoreFile = "truststore";61static String passwd = "passphrase";6263/*64* Is the server ready to serve?65*/66volatile static boolean serverReady = false;6768/*69* Turn on SSL debugging?70*/71static boolean debug = false;7273private SSLServerSocket sslServerSocket = null;7475/*76* Define the server side of the test.77*78* If the server prematurely exits, serverReady will be set to true79* to avoid infinite hangs.80*/81void doServerSide() throws Exception {82SSLServerSocketFactory sslssf =83(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();84sslServerSocket =85(SSLServerSocket) sslssf.createServerSocket(serverPort);86serverPort = sslServerSocket.getLocalPort();8788/*89* Signal Client, we're ready for his connect.90*/91serverReady = true;92SSLSocket sslSocket = null;93try {94for (int i = 0; i < 2; i++) {95sslSocket = (SSLSocket) sslServerSocket.accept();96// read request97InputStream is = sslSocket.getInputStream ();98BufferedReader r = new BufferedReader(new InputStreamReader(is));99boolean flag = false;100String x;101while ((x=r.readLine()) != null) {102if (x.length() ==0) {103break;104}105}106107PrintStream out = new PrintStream(108new BufferedOutputStream(109sslSocket.getOutputStream() ));110111/* send the header */112out.print("HTTP/1.1 200 OK\r\n");113out.print("Content-Type: text/html; charset=iso-8859-1\r\n");114out.print("Content-Length: "+10+"\r\n");115out.print("\r\n");116out.print("Testing"+i+"\r\n");117out.flush();118sslSocket.close();119}120121122sslServerSocket.close();123} catch (Exception e) {124e.printStackTrace();125}126}127128/*129* Define the client side of the test.130*131* If the server prematurely exits, serverReady will be set to true132* to avoid infinite hangs.133*/134void doClientSide() throws Exception {135HostnameVerifier reservedHV =136HttpsURLConnection.getDefaultHostnameVerifier();137try {138/*139* Wait for server to get started.140*/141while (!serverReady) {142Thread.sleep(50);143}144try {145HttpsURLConnection http = null;146/* establish http connection to server */147URL url = new URL("https://localhost:" + serverPort+"/file1");148System.out.println("url is "+url.toString());149HttpsURLConnection.setDefaultHostnameVerifier(150new NameVerifier());151http = (HttpsURLConnection)url.openConnection();152int respCode = http.getResponseCode();153int cl = http.getContentLength();154InputStream is = http.getInputStream ();155int count = 0;156while (is.read() != -1 && count++ < cl);157System.out.println("respCode1 = "+respCode);158Thread.sleep(2000);159url = new URL("https://localhost:" + serverPort+"/file2");160http = (HttpsURLConnection)url.openConnection();161respCode = http.getResponseCode();162System.out.println("respCode2 = "+respCode);163} catch (IOException ioex) {164if (sslServerSocket != null)165sslServerSocket.close();166throw ioex;167}168} finally {169HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);170}171}172173static class NameVerifier implements HostnameVerifier {174public boolean verify(String hostname, SSLSession session) {175return true;176}177}178179/*180* =============================================================181* The remainder is just support stuff182*/183184// use any free port by default185volatile int serverPort = 0;186187volatile Exception serverException = null;188volatile Exception clientException = null;189190public static void main(String args[]) throws Exception {191String keyFilename =192System.getProperty("test.src", "./") + "/" + pathToStores +193"/" + keyStoreFile;194String trustFilename =195System.getProperty("test.src", "./") + "/" + pathToStores +196"/" + trustStoreFile;197198System.setProperty("javax.net.ssl.keyStore", keyFilename);199System.setProperty("javax.net.ssl.keyStorePassword", passwd);200System.setProperty("javax.net.ssl.trustStore", trustFilename);201System.setProperty("javax.net.ssl.trustStorePassword", passwd);202203if (debug)204System.setProperty("javax.net.debug", "all");205206/*207* Start the tests.208*/209new RetryHttps();210}211212Thread clientThread = null;213Thread serverThread = null;214/*215* Primary constructor, used to drive remainder of the test.216*217* Fork off the other side, then do your work.218*/219RetryHttps() throws Exception {220if (separateServerThread) {221startServer(true);222startClient(false);223} else {224startClient(true);225startServer(false);226}227228/*229* Wait for other side to close down.230*/231if (separateServerThread) {232serverThread.join();233} else {234clientThread.join();235}236237/*238* When we get here, the test is pretty much over.239*240* If the main thread excepted, that propagates back241* immediately. If the other thread threw an exception, we242* should report back.243*/244if (serverException != null)245throw serverException;246if (clientException != null)247throw clientException;248}249250void startServer(boolean newThread) throws Exception {251if (newThread) {252serverThread = new Thread() {253public void run() {254try {255doServerSide();256} catch (Exception e) {257/*258* Our server thread just died.259*260* Release the client, if not active already...261*/262System.err.println("Server died...");263serverReady = true;264serverException = e;265}266}267};268serverThread.start();269} else {270doServerSide();271}272}273274void startClient(boolean newThread) throws Exception {275if (newThread) {276clientThread = new Thread() {277public void run() {278try {279doClientSide();280} catch (Exception e) {281/*282* Our client thread just died.283*/284System.err.println("Client died...");285clientException = e;286}287}288};289clientThread.start();290} else {291doClientSide();292}293}294}295296297