/* * Copyright (c) 2019, 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.
*/
System.out.printf("%s - received request on : %s%n", Thread.currentThread().getName(),
he.getRequestURI());
System.out.printf("%s - received request headers : %s%n", Thread.currentThread().getName(), new HashMap(he.getRequestHeaders()));
InputStream requestBody = he.getRequestBody();
String body = B8209178.toString(requestBody);
System.out.printf("%s - received request body : %s%n", Thread.currentThread().getName(), body);
publicvoid test(HttpServer server /*, HttpClient.Version version*/) throws IOException {
System.out.println("System property retryPost: " + RETRYPOST);
System.out.println("Server is: " + server.getAddress());
System.out.println("Verifying communication with server");
URI uri = URIBuilder.newBuilder()
.scheme("https")
.host(server.getAddress().getAddress())
.port(server.getAddress().getPort())
.path(PATH + "x")
.buildUnchecked();
TunnelingProxy proxy = new TunnelingProxy(server);
proxy.start();
try {
System.out.println("Proxy started");
Proxy p = new Proxy(Proxy.Type.HTTP,
InetSocketAddress.createUnresolved("localhost", proxy.getAddress().getPort()));
System.out.println("Verifying communication with proxy");
// Pipe the input stream to the output stream privatesynchronizedThread pipe(InputStream is, OutputStream os, char tag) { returnnewThread("TunnelPipe(" + tag + ")") {
@Override publicvoid run() { try { try { int c; while ((c = is.read()) != -1) {
os.write(c);
os.flush(); // if DEBUG prints a + or a - for each transferred // character. if (DEBUG) System.out.print(tag);
}
is.close();
} finally {
os.close();
}
} catch (IOException ex) { if (DEBUG) ex.printStackTrace(System.out);
}
}
};
}
public InetSocketAddress getAddress() { returnnew InetSocketAddress(ss.getInetAddress(), ss.getLocalPort());
}
// This is a bit shaky. It doesn't handle continuation // lines, but our client shouldn't send any. // Read a line from the input stream, swallowing the final // \r\n sequence. Stops at the first \n, doesn't complain // if it wasn't preceded by '\r'. //
String readLine(InputStream r) throws IOException {
StringBuilder b = new StringBuilder(); int c; while ((c = r.read()) != -1) { if (c == '\n') { break;
}
b.appendCodePoint(c);
} if (b.codePointAt(b.length() - 1) == '\r') {
b.delete(b.length() - 1, b.length());
} return b.toString();
}
publicvoid accept() {
Socket clientConnection = null; try { while (true) {
System.out.println("Tunnel: Waiting for client");
Socket previous = clientConnection; try {
clientConnection = ss.accept();
} catch (IOException io) { if (DEBUG) io.printStackTrace(System.out); break;
} finally { // we have only 1 client at a time, so it is safe // to close the previous connection here if (previous != null) previous.close();
}
System.out.println("Tunnel: Client accepted");
Socket targetConnection = null;
InputStream ccis = clientConnection.getInputStream();
OutputStream ccos = clientConnection.getOutputStream();
Writer w = new OutputStreamWriter(ccos, "UTF-8");
PrintWriter pw = new PrintWriter(w);
System.out.println("Tunnel: Reading request line");
String requestLine = readLine(ccis);
System.out.println("Tunnel: Request status line: " + requestLine); if (requestLine.startsWith("CONNECT ")) { // We should probably check that the next word following // CONNECT is the host:port of our HTTPS serverImpl. // Some improvement for a followup!
// Read all headers until we find the empty line that // signals the end of all headers. while (!requestLine.equals("")) {
System.out.println("Tunnel: Reading header: "
+ (requestLine = readLine(ccis)));
}
// Open target connection
targetConnection = new Socket(
serverImpl.getAddress().getAddress(),
serverImpl.getAddress().getPort());
// Then send the 200 OK response to the client
System.out.println("Tunnel: Sending "
+ "HTTP/1.1 200 OK\r\n\r\n");
pw.print("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
pw.flush();
} else { // This should not happen thrownew IOException("Tunnel: Unexpected status line: "
+ requestLine);
}
// Pipe the input stream of the client connection to the // output stream of the target connection and conversely. // Now the client and target will just talk to each other.
System.out.println("Tunnel: Starting tunnel pipes"); Thread t1 = pipe(ccis, targetConnection.getOutputStream(), '+'); Thread t2 = pipe(targetConnection.getInputStream(), ccos, '-');
t1.start();
t2.start();
// We have only 1 client... wait until it has finished before // accepting a new connection request.
System.out.println("Tunnel: Waiting for pipes to close");
t1.join();
t2.join();
System.out.println("Tunnel: Done - waiting for next client");
}
} catch (Throwable ex) { try {
ss.close();
} catch (IOException ex1) {
ex.addSuppressed(ex1);
}
ex.printStackTrace(System.err);
}
}
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 und die Messung sind noch experimentell.