Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.java
41161 views
/*1* Copyright (c) 2001, 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*/2223import java.io.*;24import java.net.*;25import java.security.KeyStore;26import javax.net.*;27import javax.net.ssl.*;2829import jdk.test.lib.net.URIBuilder;3031/*32* @test33* @bug 442307434* @modules java.base/sun.net.www35* @summary This test case is written to test the https POST through a proxy.36* There is no proxy authentication done. It includes a simple server37* that serves http POST method requests in secure channel, and a client38* that makes https POST request through a proxy.39* @library /test/lib40* @compile OriginServer.java ProxyTunnelServer.java41* @run main/othervm PostThruProxy42*/43public class PostThruProxy {4445private static final String TEST_SRC = System.getProperty("test.src", ".");46private static final int TIMEOUT = 30000;4748/*49* Where do we find the keystores?50*/51static String pathToStores = "../../../../../../javax/net/ssl/etc";52static String keyStoreFile = "keystore";53static String trustStoreFile = "truststore";54static String passwd = "passphrase";5556private static int serverPort = 0;57private static ProxyTunnelServer pserver;58private static TestServer server;59static final String RESPONSE_MSG = "Https POST thru proxy is successful";6061/*62* The TestServer implements a OriginServer that63* processes HTTP requests and responses.64*/65static class TestServer extends OriginServer {66public TestServer(ServerSocket ss) throws Exception {67super(ss);68}6970/*71* Returns an array of bytes containing the bytes for72* the data sent in the response.73*74* @return bytes for the data in the response75*/76public byte[] getBytes() {77return RESPONSE_MSG.getBytes();78}79}8081/*82* Main method to create the server and client83*/84public static void main(String args[]) throws Exception {8586String keyFilename = TEST_SRC + "/" + pathToStores + "/" + keyStoreFile;87String trustFilename = TEST_SRC + "/" + pathToStores + "/"88+ trustStoreFile;8990System.setProperty("javax.net.ssl.keyStore", keyFilename);91System.setProperty("javax.net.ssl.keyStorePassword", passwd);92System.setProperty("javax.net.ssl.trustStore", trustFilename);93System.setProperty("javax.net.ssl.trustStorePassword", passwd);9495InetAddress loopback = InetAddress.getLoopbackAddress();96boolean useSSL = true;97/*98* setup the server99*/100try {101ServerSocketFactory ssf = getServerSocketFactory(useSSL);102ServerSocket ss = ssf.createServerSocket(serverPort, 0, loopback);103ss.setSoTimeout(TIMEOUT); // 30 seconds104serverPort = ss.getLocalPort();105server = new TestServer(ss);106System.out.println("Server started at: " + ss);107} catch (Exception e) {108System.out.println("Server side failed:" +109e.getMessage());110throw e;111}112// trigger the client113try {114doClientSide();115} catch (Exception e) {116System.out.println("Client side failed: " +117e.getMessage());118throw e;119}120long connectCount = pserver.getConnectCount();121if (connectCount == 0) {122throw new AssertionError("Proxy was not used!");123} else {124System.out.println("Proxy CONNECT count: " + connectCount);125}126}127128private static ServerSocketFactory getServerSocketFactory129(boolean useSSL) throws Exception {130if (useSSL) {131// set up key manager to do server authentication132SSLContext ctx = SSLContext.getInstance("TLS");133KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");134KeyStore ks = KeyStore.getInstance("JKS");135char[] passphrase = passwd.toCharArray();136137ks.load(new FileInputStream(System.getProperty(138"javax.net.ssl.keyStore")), passphrase);139kmf.init(ks, passphrase);140ctx.init(kmf.getKeyManagers(), null, null);141142return ctx.getServerSocketFactory();143} else {144return ServerSocketFactory.getDefault();145}146}147148/*149* Message to be posted150*/151static String postMsg = "Testing HTTP post on a https server";152153static void doClientSide() throws Exception {154HostnameVerifier reservedHV =155HttpsURLConnection.getDefaultHostnameVerifier();156try {157/*158* setup up a proxy159*/160SocketAddress pAddr = setupProxy();161162/*163* we want to avoid URLspoofCheck failures in cases where the cert164* DN name does not match the hostname in the URL.165*/166HttpsURLConnection.setDefaultHostnameVerifier(167new NameVerifier());168URL url = URIBuilder.newBuilder()169.scheme("https")170.loopback()171.port(serverPort)172.toURL();173174Proxy p = new Proxy(Proxy.Type.HTTP, pAddr);175System.out.println("Client connecting to: " + url);176System.out.println("Through proxy: " + pAddr);177HttpsURLConnection https = (HttpsURLConnection)url.openConnection(p);178https.setConnectTimeout(TIMEOUT);179https.setReadTimeout(TIMEOUT);180https.setDoOutput(true);181https.setRequestMethod("POST");182PrintStream ps = null;183try {184ps = new PrintStream(https.getOutputStream());185ps.println(postMsg);186ps.flush();187if (https.getResponseCode() != 200) {188throw new RuntimeException("test Failed");189}190ps.close();191192// clear the pipe193BufferedReader in = new BufferedReader(194new InputStreamReader(195https.getInputStream()));196String inputLine;197boolean msgFound = false;198while ((inputLine = in.readLine()) != null) {199System.out.println("Client received: " + inputLine);200if (inputLine.contains(RESPONSE_MSG)) msgFound = true;201}202in.close();203if (!msgFound) {204throw new RuntimeException("POST message not found.");205}206} catch (SSLException e) {207if (ps != null)208ps.close();209throw e;210} catch (SocketTimeoutException e) {211System.out.println("Client can not get response in time: "212+ e.getMessage());213}214} finally {215HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);216}217}218219static class NameVerifier implements HostnameVerifier {220public boolean verify(String hostname, SSLSession session) {221return true;222}223}224225static SocketAddress setupProxy() throws IOException {226InetAddress loopback = InetAddress.getLoopbackAddress();227pserver = new ProxyTunnelServer(loopback);228229// disable proxy authentication230pserver.needUserAuth(false);231pserver.start();232return new InetSocketAddress(loopback, pserver.getPort());233}234235}236237238