/* * Copyright (c) 2014, 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.
*/
// Make two connections. The bug occurs when the second request is made for (int i = 0; i < 2; i++) {
System.out.println("Client: Requesting " + url.toExternalForm()
+ " via " + proxy.toString()
+ " (attempt " + (i + 1) + " of 2)");
ProxyTunnel copyToClient = new ProxyTunnel(serverSocket, clientSocket);
ProxyTunnel copyToServer = new ProxyTunnel(clientSocket, serverSocket);
copyToClient.start();
copyToServer.start();
copyToClient.join(); // here copyToClient.close() would not provoke the // bug ( since it would trigger the retry logic in // HttpURLConnction.writeRequests ), so close only // the output to get the connection in this state.
clientSocket.shutdownOutput();
// now close all connections to finish the test
copyToServer.close();
copyToClient.close();
} catch (IOException | NumberFormatException
| InterruptedException e) {
e.printStackTrace();
}
}
};
threads.add(t);
t.start();
} for (Thread t: threads)
t.join();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
/** * 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.
*/ class ProxyTunnel extendsThread { privatefinal Socket sockIn; privatefinal Socket sockOut; privatefinal InputStream input; privatefinal OutputStream output;
// Create the server that the test wants to connect to via the proxy
serverSocket = ServerSocketFactory.getDefault().createServerSocket();
serverSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
}
// write the success response, the request body is not read. // close only output and keep input open.
OutputStream out = sslSocket.getOutputStream();
out.write(serverResponse.getBytes("UTF-8"));
socket.shutdownOutput();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/** * read the header and return only the first line. * * @param inputStream the stream to read from * @return the first line of the stream * @throws IOException if reading failed
*/ privatestatic String readHeader(InputStream inputStream) throws IOException {
String line;
String firstLine = null; while ((line = readLine(inputStream)) != null && line.length() > 0) { if (firstLine == null) {
firstLine = line;
}
}
return firstLine;
}
/** * read a line from stream. * * @param inputStream the stream to read from * @return the line * @throws IOException if reading failed
*/ privatestatic String readLine(InputStream inputStream) throws IOException { final StringBuilder line = new StringBuilder(); int ch; while ((ch = inputStream.read()) != -1) { if (ch == '\r') { continue;
}
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override publicboolean verify(String hostname, SSLSession sslSession) { // ignore the cert's CN; it's not important to this test returntrue;
}
});
// Set up the socket factory to use a trust manager that trusts all // certs, since trust validation isn't important to this test final TrustManager[] trustAllCertChains = new TrustManager[] { new X509TrustManager() {
@Override public X509Certificate[] getAcceptedIssuers() { returnnull;
}
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.