Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/RetryHttps.java
41161 views
/*1* Copyright (c) 2003, 2019, 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* @library /test/lib27* @run main/othervm RetryHttps28* @run main/othervm -Djava.net.preferIPv6Addresses=true RetryHttps29*30* SunJSSE does not support dynamic system properties, no way to re-use31* system properties in samevm/agentvm mode.32* @author Yingxian Wang33*/3435import java.net.*;36import java.util.*;37import java.io.*;38import javax.net.ssl.*;39import jdk.test.lib.net.URIBuilder;4041public class RetryHttps {42static Map cookies;43ServerSocket ss;4445/*46* =============================================================47* Set the various variables needed for the tests, then48* specify what tests to run on each side.49*/5051/*52* Should we run the client or server in a separate thread?53* Both sides can throw exceptions, but do you have a preference54* as to which side should be the main thread.55*/56static boolean separateServerThread = true;5758/*59* Where do we find the keystores?60*/61static String pathToStores = "../../../../../../javax/net/ssl/etc";62static String keyStoreFile = "keystore";63static String trustStoreFile = "truststore";64static String passwd = "passphrase";6566/*67* Is the server ready to serve?68*/69volatile static boolean serverReady = false;7071/*72* Turn on SSL debugging?73*/74static boolean debug = true;7576private SSLServerSocket sslServerSocket = null;7778/*79* Define the server side of the test.80*81* If the server prematurely exits, serverReady will be set to true82* to avoid infinite hangs.83*/84void doServerSide() throws Exception {85InetAddress loopback = InetAddress.getLoopbackAddress();86SSLServerSocketFactory sslssf =87(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();88sslServerSocket =89(SSLServerSocket) sslssf.createServerSocket(serverPort, 0, loopback);90serverPort = sslServerSocket.getLocalPort();9192System.out.println("Starting server at: "93+ sslServerSocket.getInetAddress()94+ ":" + serverPort);9596/*97* Signal Client, we're ready for his connect.98*/99serverReady = true;100SSLSocket sslSocket = null;101try {102for (int i = 0; i < 2; i++) {103sslSocket = (SSLSocket) sslServerSocket.accept();104// read request105InputStream is = sslSocket.getInputStream ();106BufferedReader r = new BufferedReader(new InputStreamReader(is));107boolean flag = false;108String x;109while ((x=r.readLine()) != null) {110if (x.length() ==0) {111break;112}113}114115PrintStream out = new PrintStream(116new BufferedOutputStream(117sslSocket.getOutputStream() ));118119/* send the header */120out.print("HTTP/1.1 200 OK\r\n");121out.print("Content-Type: text/html; charset=iso-8859-1\r\n");122out.print("Content-Length: "+10+"\r\n");123out.print("\r\n");124out.print("Testing"+i+"\r\n");125out.flush();126sslSocket.close();127}128129130sslServerSocket.close();131} catch (Exception e) {132e.printStackTrace();133}134}135136/*137* Define the client side of the test.138*139* If the server prematurely exits, serverReady will be set to true140* to avoid infinite hangs.141*/142void doClientSide() throws Exception {143HostnameVerifier reservedHV =144HttpsURLConnection.getDefaultHostnameVerifier();145try {146/*147* Wait for server to get started.148*/149while (!serverReady) {150Thread.sleep(50);151}152try {153HttpsURLConnection http = null;154/* establish http connection to server */155URL url = URIBuilder.newBuilder()156.scheme("https")157.loopback()158.port(serverPort)159.path("/file1")160.toURL();161System.out.println("url is "+url.toString());162HttpsURLConnection.setDefaultHostnameVerifier(163new NameVerifier());164http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);165int respCode = http.getResponseCode();166int cl = http.getContentLength();167InputStream is = http.getInputStream ();168int count = 0;169while (is.read() != -1 && count++ < cl);170System.out.println("respCode1 = "+respCode);171Thread.sleep(2000);172url = URIBuilder.newBuilder()173.scheme("https")174.loopback()175.port(serverPort)176.path("/file2")177.toURL();178http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);179respCode = http.getResponseCode();180System.out.println("respCode2 = "+respCode);181} catch (IOException ioex) {182if (sslServerSocket != null)183sslServerSocket.close();184throw ioex;185}186} finally {187HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);188}189}190191static class NameVerifier implements HostnameVerifier {192public boolean verify(String hostname, SSLSession session) {193return true;194}195}196197/*198* =============================================================199* The remainder is just support stuff200*/201202// use any free port by default203volatile int serverPort = 0;204205volatile Exception serverException = null;206volatile Exception clientException = null;207208public static void main(String args[]) throws Exception {209String keyFilename =210System.getProperty("test.src", "./") + "/" + pathToStores +211"/" + keyStoreFile;212String trustFilename =213System.getProperty("test.src", "./") + "/" + pathToStores +214"/" + trustStoreFile;215216System.setProperty("javax.net.ssl.keyStore", keyFilename);217System.setProperty("javax.net.ssl.keyStorePassword", passwd);218System.setProperty("javax.net.ssl.trustStore", trustFilename);219System.setProperty("javax.net.ssl.trustStorePassword", passwd);220221if (debug)222System.setProperty("javax.net.debug", "all");223224/*225* Start the tests.226*/227new RetryHttps();228}229230Thread clientThread = null;231Thread serverThread = null;232/*233* Primary constructor, used to drive remainder of the test.234*235* Fork off the other side, then do your work.236*/237RetryHttps() throws Exception {238if (separateServerThread) {239startServer(true);240startClient(false);241} else {242startClient(true);243startServer(false);244}245246/*247* Wait for other side to close down.248*/249if (separateServerThread) {250serverThread.join();251} else {252clientThread.join();253}254255/*256* When we get here, the test is pretty much over.257*258* If the main thread excepted, that propagates back259* immediately. If the other thread threw an exception, we260* should report back.261*/262if (serverException != null)263throw serverException;264if (clientException != null)265throw clientException;266}267268void startServer(boolean newThread) throws Exception {269if (newThread) {270serverThread = new Thread() {271public void run() {272try {273doServerSide();274} catch (Exception e) {275/*276* Our server thread just died.277*278* Release the client, if not active already...279*/280System.err.println("Server died...");281serverReady = true;282serverException = e;283}284}285};286serverThread.start();287} else {288doServerSide();289}290}291292void startClient(boolean newThread) throws Exception {293if (newThread) {294clientThread = new Thread() {295public void run() {296try {297doClientSide();298} catch (Exception e) {299/*300* Our client thread just died.301*/302System.err.println("Client died...");303clientException = e;304}305}306};307clientThread.start();308} else {309doClientSide();310}311}312}313314315