in src/main/java/org/apache/commons/net/ftp/FTPHTTPClient.java [185:233]
private BufferedReader tunnelHandshake(final String host, final int port, final InputStream input, final OutputStream output)
throws IOException, UnsupportedEncodingException {
final String connectString = "CONNECT " + host + ":" + port + " HTTP/1.1";
final String hostString = "Host: " + host + ":" + port;
this.tunnelHost = host;
output.write(connectString.getBytes(charset));
output.write(CRLF);
output.write(hostString.getBytes(charset));
output.write(CRLF);
if (proxyUsername != null && proxyPassword != null) {
final String auth = proxyUsername + ":" + proxyPassword;
final String header = "Proxy-Authorization: Basic " + base64.encodeToString(auth.getBytes(charset));
output.write(header.getBytes(charset));
}
output.write(CRLF);
final List<String> response = new ArrayList<>();
final BufferedReader reader = new BufferedReader(new InputStreamReader(input, getCharset()));
for (String line = reader.readLine(); line != null && !line.isEmpty(); line = reader.readLine()) {
response.add(line);
}
final int size = response.size();
if (size == 0) {
throw new IOException("No response from proxy");
}
String code;
final String resp = response.get(0);
if (!resp.startsWith("HTTP/") || (resp.length() < 12)) {
throw new IOException("Invalid response from proxy: " + resp);
}
code = resp.substring(9, 12);
if (!"200".equals(code)) {
final StringBuilder msg = new StringBuilder();
msg.append("HTTPTunnelConnector: connection failed\r\n");
msg.append("Response received from the proxy:\r\n");
for (final String line : response) {
msg.append(line);
msg.append("\r\n");
}
throw new IOException(msg.toString());
}
return reader;
}