diff --git a/src/main/java/com/hierynomus/sshj/backport/Jdk7HttpProxySocket.java b/src/main/java/com/hierynomus/sshj/backport/Jdk7HttpProxySocket.java deleted file mode 100644 index 56ea256e6..000000000 --- a/src/main/java/com/hierynomus/sshj/backport/Jdk7HttpProxySocket.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (C)2009 - SSHJ Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.hierynomus.sshj.backport; - -import net.schmizz.sshj.common.IOUtils; - -import java.io.IOException; -import java.io.InputStream; -import java.net.*; - -public class Jdk7HttpProxySocket extends Socket { - - private Proxy httpProxy = null; - - public Jdk7HttpProxySocket(Proxy proxy) { - super(proxy.type() == Proxy.Type.HTTP ? Proxy.NO_PROXY : proxy); - if (proxy.type() == Proxy.Type.HTTP) { - this.httpProxy = proxy; - } - } - - @Override - public void connect(SocketAddress endpoint, int timeout) throws IOException { - if (httpProxy != null) { - connectHttpProxy(endpoint, timeout); - } else { - super.connect(endpoint, timeout); - } - } - - private void connectHttpProxy(SocketAddress endpoint, int timeout) throws IOException { - super.connect(httpProxy.address(), timeout); - - if (!(endpoint instanceof InetSocketAddress)) { - throw new SocketException("Expected an InetSocketAddress to connect to, got: " + endpoint); - } - InetSocketAddress isa = (InetSocketAddress) endpoint; - String httpConnect = "CONNECT " + isa.getHostName() + ":" + isa.getPort() + " HTTP/1.0\n\n"; - getOutputStream().write(httpConnect.getBytes(IOUtils.UTF8)); - checkAndFlushProxyResponse(); - } - - private void checkAndFlushProxyResponse()throws IOException { - InputStream socketInput = getInputStream(); - byte[] tmpBuffer = new byte[512]; - int len = socketInput.read(tmpBuffer, 0, tmpBuffer.length); - - if (len == 0) { - throw new SocketException("Empty response from proxy"); - } - - String proxyResponse = new String(tmpBuffer, 0, len, IOUtils.UTF8); - - // Expecting HTTP/1.x 200 OK - if (proxyResponse.contains("200")) { - // Flush any outstanding message in buffer - if (socketInput.available() > 0) { - socketInput.skip(socketInput.available()); - } - // Proxy Connect Successful - } else { - throw new SocketException("Fail to create Socket\nResponse was:" + proxyResponse); - } - } -} diff --git a/src/main/java/com/hierynomus/sshj/backport/Sockets.java b/src/main/java/com/hierynomus/sshj/backport/Sockets.java deleted file mode 100644 index 797691cc2..000000000 --- a/src/main/java/com/hierynomus/sshj/backport/Sockets.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C)2009 - SSHJ Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.hierynomus.sshj.backport; - -import java.io.Closeable; -import java.io.IOException; -import java.net.Socket; - -public class Sockets { - - /** - * Java 7 and up have Socket implemented as Closeable, whereas Java6 did not have this inheritance. - * @param socket The socket to wrap as Closeable - * @return The (potentially wrapped) Socket as a Closeable. - */ - public static Closeable asCloseable(final Socket socket) { - if (Closeable.class.isAssignableFrom(socket.getClass())) { - return Closeable.class.cast(socket); - } else { - return new Closeable() { - @Override - public void close() throws IOException { - socket.close(); - } - }; - } - } -} diff --git a/src/main/java/net/schmizz/sshj/connection/channel/SocketStreamCopyMonitor.java b/src/main/java/net/schmizz/sshj/connection/channel/SocketStreamCopyMonitor.java index 49a69df27..29c598c7d 100644 --- a/src/main/java/net/schmizz/sshj/connection/channel/SocketStreamCopyMonitor.java +++ b/src/main/java/net/schmizz/sshj/connection/channel/SocketStreamCopyMonitor.java @@ -22,8 +22,6 @@ import java.net.Socket; import java.util.concurrent.TimeUnit; -import static com.hierynomus.sshj.backport.Sockets.asCloseable; - public class SocketStreamCopyMonitor extends Thread { @@ -43,7 +41,7 @@ public void run() { await(y); } catch (IOException ignored) { } finally { - IOUtils.closeQuietly(channel, asCloseable(socket)); + IOUtils.closeQuietly(channel, socket); } } diff --git a/src/main/java/net/schmizz/sshj/connection/channel/direct/LocalPortForwarder.java b/src/main/java/net/schmizz/sshj/connection/channel/direct/LocalPortForwarder.java index 287277d4a..53a8178b0 100644 --- a/src/main/java/net/schmizz/sshj/connection/channel/direct/LocalPortForwarder.java +++ b/src/main/java/net/schmizz/sshj/connection/channel/direct/LocalPortForwarder.java @@ -29,8 +29,6 @@ import java.net.SocketException; import java.util.concurrent.TimeUnit; -import static com.hierynomus.sshj.backport.Sockets.asCloseable; - public class LocalPortForwarder { public static class ForwardedChannel @@ -78,7 +76,7 @@ private void startChannel(Socket socket) throws IOException { chan.open(); chan.start(); } catch (IOException e) { - IOUtils.closeQuietly(chan, asCloseable(socket)); + IOUtils.closeQuietly(chan, socket); throw e; } }