/* * Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * @test * @bug 6370908 8220663 * @library /test/lib * @summary Add support for HTTP_CONNECT proxy in Socket class. * This test uses the wildcard address and is susceptible to fail intermittently. * @key intermittent * @run main HttpProxy * @run main/othervm -Djava.net.preferIPv4Stack=true HttpProxy * @run main/othervm -Djava.net.preferIPv6Addresses=true HttpProxy
*/
void test() throws Exception {
InetSocketAddress proxyAddress = new InetSocketAddress(proxyHost, proxyPort);
Proxy httpProxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
// Wildcard address is needed here try (ServerSocket ss = new ServerSocket(0)) {
List<InetSocketAddress> externalAddresses = new ArrayList<>();
externalAddresses.add( new InetSocketAddress(InetAddress.getLocalHost(), ss.getLocalPort()));
if (canUseIPv6()) { byte[] bytes = newbyte[] {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; var address = InetAddress.getByAddress(bytes);
externalAddresses.add( new InetSocketAddress(address, ss.getLocalPort()));
}
for (SocketAddress externalAddress : externalAddresses) { try (Socket sock = new Socket(httpProxy)) {
sock.setSoTimeout(SO_TIMEOUT);
sock.setTcpNoDelay(false);
out.println("Trying to connect to server socket on " + externalAddress);
sock.connect(externalAddress); try (Socket externalSock = ss.accept()) { // perform some simple checks
check(sock.isBound(), "Socket is not bound");
check(sock.isConnected(), "Socket is not connected");
check(!sock.isClosed(), "Socket should not be closed");
check(sock.getSoTimeout() == SO_TIMEOUT, "Socket should have a previously set timeout");
check(sock.getTcpNoDelay() == false, "NODELAY should be false");
simpleDataExchange(sock, externalSock);
}
}
}
}
}
staticvoid check(boolean condition, String message) { if (!condition) out.println(message);
}
// performs a simple exchange of data between the two sockets // and throws an exception if there is any problem. void simpleDataExchange(Socket s1, Socket s2) throws Exception { try (final InputStream i1 = s1.getInputStream(); final InputStream i2 = s2.getInputStream(); final OutputStream o1 = s1.getOutputStream(); final OutputStream o2 = s2.getOutputStream()) {
startSimpleWriter("simpleWriter1", o1, 100);
startSimpleWriter("simpleWriter2", o2, 200);
simpleRead(i2, 100);
simpleRead(i1, 200);
}
}
if (!statusLine.startsWith("CONNECT")) {
out.println("proxy server: processes only "
+ "CONNECT method requests, received: "
+ statusLine); return;
}
// retrieve the host and port info from the status-line
InetSocketAddress serverAddr = getConnectInfo(statusLine);
out.println("Proxy serving CONNECT request to " + serverAddr);
//open socket to the server try (Socket serverSocket = new Socket(serverAddr.getAddress(),
serverAddr.getPort())) {
Forwarder clientFW = new Forwarder(clientSocket.getInputStream(),
serverSocket.getOutputStream()); Thread clientForwarderThread = newThread(clientFW, "ClientForwarder");
clientForwarderThread.start();
send200(clientSocket);
Forwarder serverFW = new Forwarder(serverSocket.getInputStream(),
clientSocket.getOutputStream());
serverFW.run();
clientForwarderThread.join();
}
}
privatevoid send200(Socket clientSocket) throws IOException {
OutputStream out = clientSocket.getOutputStream();
PrintWriter pout = new PrintWriter(out);
/* * This method retrieves the hostname and port of the tunnel destination * from the request line. * @param connectStr * of the form: <i>CONNECT server-name:server-port HTTP/1.x</i>
*/ static InetSocketAddress getConnectInfo(String connectStr) throws Exception
{ try { int starti = connectStr.indexOf(' '); int endi = connectStr.lastIndexOf(' ');
String connectInfo = connectStr.substring(starti+1, endi).trim(); // retrieve server name and port
endi = connectInfo.lastIndexOf(':');
String name = connectInfo.substring(0, endi);
if (name.contains(":")) { if (!(name.startsWith("[") && name.endsWith("]"))) { thrownew IOException("Invalid host:" + name);
}
name = name.substring(1, name.length() - 1);
} int port = Integer.parseInt(connectInfo.substring(endi+1)); returnnew InetSocketAddress(name, port);
} catch (Exception e) {
out.println("Proxy received a request: " + connectStr); throw unexpected(e);
}
}
}
/* Reads from the given InputStream and writes to the given OutputStream */ staticclass Forwarder implements Runnable
{ privatefinal InputStream in; privatefinal OutputStream os;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.