Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.java
38889 views
/*1* Copyright (c) 2001, 2016, 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.testlibrary.OutputAnalyzer;30import jdk.testlibrary.ProcessTools;3132/*33* @test34* @bug 442307435* @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 /lib/testlibrary40* @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;5758/*59* The TestServer implements a OriginServer that60* processes HTTP requests and responses.61*/62static class TestServer extends OriginServer {63public TestServer(ServerSocket ss) throws Exception {64super(ss);65}6667/*68* Returns an array of bytes containing the bytes for69* the data sent in the response.70*71* @return bytes for the data in the response72*/73public byte[] getBytes() {74return "Https POST thru proxy is successful".75getBytes();76}77}7879/*80* Main method to create the server and client81*/82public static void main(String args[]) throws Exception {83String keyFilename = TEST_SRC + "/" + pathToStores + "/" + keyStoreFile;84String trustFilename = TEST_SRC + "/" + pathToStores + "/"85+ trustStoreFile;8687System.setProperty("javax.net.ssl.keyStore", keyFilename);88System.setProperty("javax.net.ssl.keyStorePassword", passwd);89System.setProperty("javax.net.ssl.trustStore", trustFilename);90System.setProperty("javax.net.ssl.trustStorePassword", passwd);9192boolean useSSL = true;93/*94* setup the server95*/96try {97ServerSocketFactory ssf = getServerSocketFactory(useSSL);98ServerSocket ss = ssf.createServerSocket(serverPort);99ss.setSoTimeout(TIMEOUT); // 30 seconds100serverPort = ss.getLocalPort();101new TestServer(ss);102} catch (Exception e) {103System.out.println("Server side failed:" +104e.getMessage());105throw e;106}107// trigger the client108try {109doClientSide();110} catch (Exception e) {111System.out.println("Client side failed: " +112e.getMessage());113throw e;114}115}116117private static ServerSocketFactory getServerSocketFactory118(boolean useSSL) throws Exception {119if (useSSL) {120// set up key manager to do server authentication121SSLContext ctx = SSLContext.getInstance("TLS");122KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");123KeyStore ks = KeyStore.getInstance("JKS");124char[] passphrase = passwd.toCharArray();125126ks.load(new FileInputStream(System.getProperty(127"javax.net.ssl.keyStore")), passphrase);128kmf.init(ks, passphrase);129ctx.init(kmf.getKeyManagers(), null, null);130131return ctx.getServerSocketFactory();132} else {133return ServerSocketFactory.getDefault();134}135}136137/*138* Message to be posted139*/140static String postMsg = "Testing HTTP post on a https server";141142static void doClientSide() throws Exception {143HostnameVerifier reservedHV =144HttpsURLConnection.getDefaultHostnameVerifier();145try {146/*147* setup up a proxy148*/149SocketAddress pAddr = setupProxy();150151/*152* we want to avoid URLspoofCheck failures in cases where the cert153* DN name does not match the hostname in the URL.154*/155HttpsURLConnection.setDefaultHostnameVerifier(156new NameVerifier());157URL url = new URL("https://" + getHostname() +":" + serverPort);158159Proxy p = new Proxy(Proxy.Type.HTTP, pAddr);160HttpsURLConnection https = (HttpsURLConnection)url.openConnection(p);161https.setConnectTimeout(TIMEOUT);162https.setReadTimeout(TIMEOUT);163https.setDoOutput(true);164https.setRequestMethod("POST");165PrintStream ps = null;166try {167ps = new PrintStream(https.getOutputStream());168ps.println(postMsg);169ps.flush();170if (https.getResponseCode() != 200) {171throw new RuntimeException("test Failed");172}173ps.close();174175// clear the pipe176BufferedReader in = new BufferedReader(177new InputStreamReader(178https.getInputStream()));179String inputLine;180while ((inputLine = in.readLine()) != null)181System.out.println("Client received: " + inputLine);182in.close();183} catch (SSLException e) {184if (ps != null)185ps.close();186throw e;187} catch (SocketTimeoutException e) {188System.out.println("Client can not get response in time: "189+ e.getMessage());190}191} finally {192HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);193}194}195196static class NameVerifier implements HostnameVerifier {197public boolean verify(String hostname, SSLSession session) {198return true;199}200}201202static SocketAddress setupProxy() throws IOException {203ProxyTunnelServer pserver = new ProxyTunnelServer();204205// disable proxy authentication206pserver.needUserAuth(false);207pserver.start();208return new InetSocketAddress("localhost", pserver.getPort());209}210211private static String getHostname() {212try {213OutputAnalyzer oa = ProcessTools.executeCommand("hostname");214return oa.getOutput().trim();215} catch (Throwable e) {216throw new RuntimeException("Get hostname failed.", e);217}218}219}220221222