diff --git a/src/main/java/net/schmizz/sshj/common/StreamCopier.java b/src/main/java/net/schmizz/sshj/common/StreamCopier.java index 344df039..424d8bf4 100644 --- a/src/main/java/net/schmizz/sshj/common/StreamCopier.java +++ b/src/main/java/net/schmizz/sshj/common/StreamCopier.java @@ -145,8 +145,14 @@ public class StreamCopier { final double sizeKiB = count / 1024.0; log.debug(String.format("%1$,.1f KiB transferred in %2$,.1f seconds (%3$,.2f KiB/s)", sizeKiB, timeSeconds, (sizeKiB / timeSeconds))); - if (length != -1 && read == -1) - throw new IOException("Encountered EOF, could not transfer " + length + " bytes"); + // Did we encounter EOF? + if (read == -1) { + // If InputStream was closed we should also close OutputStream + out.close(); + + if (length != -1) + throw new IOException("Encountered EOF, could not transfer " + length + " bytes"); + } return count; } diff --git a/src/test/java/com/hierynomus/sshj/connection/channel/forwarded/LocalPortForwarderTest.java b/src/test/java/com/hierynomus/sshj/connection/channel/forwarded/LocalPortForwarderTest.java new file mode 100644 index 00000000..fd48b886 --- /dev/null +++ b/src/test/java/com/hierynomus/sshj/connection/channel/forwarded/LocalPortForwarderTest.java @@ -0,0 +1,139 @@ +/* + * 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.connection.channel.forwarded; + +import com.hierynomus.sshj.test.HttpServer; +import com.hierynomus.sshj.test.SshFixture; +import com.hierynomus.sshj.test.util.FileUtil; +import net.schmizz.sshj.SSHClient; +import net.schmizz.sshj.connection.channel.direct.LocalPortForwarder; +import net.schmizz.sshj.connection.channel.direct.Parameters; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.sshd.server.forward.AcceptAllForwardingFilter; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + +public class LocalPortForwarderTest { + private static final Logger log = LoggerFactory.getLogger(LocalPortForwarderTest.class); + + @Rule + public SshFixture fixture = new SshFixture(); + + @Rule + public HttpServer httpServer = new HttpServer(); + + @Before + public void setUp() throws IOException { + fixture.getServer().setForwardingFilter(new AcceptAllForwardingFilter()); + File file = httpServer.getDocRoot().newFile("index.html"); + FileUtil.writeToFile(file, "