/* * Copyright (c) 2003, 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 4920526 * @summary Needs per connection proxy support for URLs * @library /test/lib * @compile PerConnectionProxy.java * @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 PerConnectionProxy
*/
// for non existing proxy expect an IOException try {
InetSocketAddress isa = InetSocketAddress.createUnresolved("inexistent", 8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP, isa);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (proxy);
InputStream is = urlc.getInputStream ();
is.close(); thrownew RuntimeException("non existing per connection proxy should lead to IOException");
} catch (IOException ioex) { // expected
} // for NO_PROXY, expect direct connection try {
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (Proxy.NO_PROXY); int respCode = urlc.getResponseCode();
urlc.disconnect();
} catch (IOException ioex) { thrownew RuntimeException("direct connection should succeed :"+ioex.getMessage());
} // for a normal proxy setting expect to see connection // goes through that proxy try {
InetSocketAddress isa = InetSocketAddress.createUnresolved(
loopbackAddress.getHostAddress(),
pserver.getPort());
Proxy p = new Proxy(Proxy.Type.HTTP, isa);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (p); int respCode = urlc.getResponseCode();
urlc.disconnect();
} catch (IOException ioex) { thrownew RuntimeException("connection through a local proxy should succeed :"+ioex.getMessage());
}
staticclass ProxyServer extendsThread { privatestatic ServerSocket ss = null;
// client requesting for a tunnel private Socket clientSocket = null;
/* * Origin server's address and port that the client * wants to establish the tunnel for communication.
*/ private InetAddress serverInetAddr; privateint serverPort;
public ProxyServer(InetAddress server, int port) throws IOException {
serverInetAddr = server;
serverPort = port;
ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
}
/** *************************************************************** * helper methods follow ***************************************************************
*/ publicint getPort() { return ss.getLocalPort();
} /* * This inner class provides unidirectional data flow through the sockets * by continuously copying bytes from the input socket onto the output * socket, until both sockets are open and EOF has not been received.
*/ staticclass ProxyTunnel extendsThread {
Socket sockIn;
Socket sockOut;
InputStream input;
OutputStream output;
publicvoid run() { int BUFFER_SIZE = 400; byte[] buf = newbyte[BUFFER_SIZE]; int bytesRead = 0; int count = 0; // keep track of the amount of data transfer
try { while ((bytesRead = input.read(buf)) >= 0) {
output.write(buf, 0, bytesRead);
output.flush();
count += bytesRead;
}
} catch (IOException e) { /* * The peer end has closed the connection * we will close the tunnel
*/
close();
}
}
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.