Skip to content

Commit 857d56a

Browse files
Replaced Grizzly HTTP Server with Java HTTP Server (#1010)
Co-authored-by: Jeroen van Erp <[email protected]>
1 parent 0e4a8f6 commit 857d56a

File tree

4 files changed

+38
-48
lines changed

4 files changed

+38
-48
lines changed

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ testing {
9898
implementation "org.apache.sshd:sshd-sftp:$sshdVersion"
9999
implementation "org.apache.sshd:sshd-scp:$sshdVersion"
100100
implementation "ch.qos.logback:logback-classic:1.5.18"
101-
implementation 'org.glassfish.grizzly:grizzly-http-server:3.0.1'
102101
}
103102

104103
targets {

src/test/java/com/hierynomus/sshj/connection/channel/forwarded/LocalPortForwarderTest.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.hierynomus.sshj.test.HttpServer;
1919
import com.hierynomus.sshj.test.SshServerExtension;
20-
import com.hierynomus.sshj.test.util.FileUtil;
2120
import net.schmizz.sshj.SSHClient;
2221
import net.schmizz.sshj.connection.channel.direct.LocalPortForwarder;
2322
import net.schmizz.sshj.connection.channel.direct.Parameters;
@@ -29,35 +28,30 @@
2928

3029
import java.io.*;
3130
import java.net.*;
32-
import java.nio.file.Files;
3331

3432
import static org.junit.jupiter.api.Assertions.assertEquals;
3533

3634
public class LocalPortForwarderTest {
37-
private static final String LOCALHOST_URL = "http://127.0.0.1:8080";
38-
3935
@RegisterExtension
4036
public SshServerExtension fixture = new SshServerExtension();
4137

4238
@RegisterExtension
4339
public HttpServer httpServer = new HttpServer();
4440

4541
@BeforeEach
46-
public void setUp() throws IOException {
42+
public void setUp() {
4743
fixture.getServer().setForwardingFilter(new AcceptAllForwardingFilter());
48-
File file = Files.createFile(httpServer.getDocRoot().toPath().resolve("index.html")).toFile();
49-
FileUtil.writeToFile(file, "<html><head/><body><h1>Hi!</h1></body></html>");
5044
}
5145

5246
@Test
5347
public void shouldHaveWorkingHttpServer() throws IOException {
54-
assertEquals(200, httpGet());
48+
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, httpGet());
5549
}
5650

5751
@Test
5852
public void shouldHaveHttpServerThatClosesConnectionAfterResponse() throws IOException {
5953
// Just to check that the test server does close connections before we try through the forwarder...
60-
httpGetAndAssertConnectionClosedByServer(8080);
54+
httpGetAndAssertConnectionClosedByServer(httpServer.getServerUrl().getPort());
6155
}
6256

6357
@Test
@@ -68,7 +62,8 @@ public void shouldCloseConnectionWhenRemoteServerClosesConnection() throws IOExc
6862
ServerSocket serverSocket = new ServerSocket();
6963
serverSocket.setReuseAddress(true);
7064
serverSocket.bind(new InetSocketAddress("0.0.0.0", 12345));
71-
LocalPortForwarder localPortForwarder = sshClient.newLocalPortForwarder(new Parameters("0.0.0.0", 12345, "localhost", 8080), serverSocket);
65+
final int serverPort = httpServer.getServerUrl().getPort();
66+
LocalPortForwarder localPortForwarder = sshClient.newLocalPortForwarder(new Parameters("0.0.0.0", 12345, "localhost", serverPort), serverSocket);
7267
new Thread(() -> {
7368
try {
7469
localPortForwarder.listen();
@@ -90,7 +85,7 @@ public static void httpGetAndAssertConnectionClosedByServer(int port) throws IOE
9085
// It returns 400 Bad Request because it's missing a bunch of info, but the HTTP response doesn't matter, we just want to test the connection closing.
9186
OutputStream outputStream = socket.getOutputStream();
9287
PrintWriter writer = new PrintWriter(outputStream);
93-
writer.println("GET / HTTP/1.1");
88+
writer.println("GET / HTTP/1.1\r\n");
9489
writer.println("");
9590
writer.flush();
9691

@@ -111,7 +106,7 @@ public static void httpGetAndAssertConnectionClosedByServer(int port) throws IOE
111106
}
112107

113108
private int httpGet() throws IOException {
114-
final URL url = new URL(LOCALHOST_URL);
109+
final URL url = httpServer.getServerUrl().toURL();
115110
final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
116111
urlConnection.setConnectTimeout(3000);
117112
urlConnection.setRequestMethod("GET");

src/test/java/com/hierynomus/sshj/connection/channel/forwarded/RemotePortForwarderTest.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.hierynomus.sshj.test.HttpServer;
1919
import com.hierynomus.sshj.test.SshServerExtension;
20-
import com.hierynomus.sshj.test.util.FileUtil;
2120
import net.schmizz.sshj.SSHClient;
2221
import net.schmizz.sshj.connection.ConnectionException;
2322
import net.schmizz.sshj.connection.channel.forwarded.RemotePortForwarder;
@@ -27,20 +26,18 @@
2726
import org.junit.jupiter.api.Test;
2827
import org.junit.jupiter.api.extension.RegisterExtension;
2928

30-
import java.io.File;
3129
import java.io.IOException;
3230
import java.net.HttpURLConnection;
3331
import java.net.InetSocketAddress;
32+
import java.net.URI;
3433
import java.net.URL;
35-
import java.nio.file.Files;
3634

3735
import static org.junit.jupiter.api.Assertions.assertEquals;
3836

3937
public class RemotePortForwarderTest {
4038
private static final PortRange RANGE = new PortRange(9000, 9999);
4139
private static final String LOCALHOST = "127.0.0.1";
42-
private static final String LOCALHOST_URL_FORMAT = "http://127.0.0.1:%d";
43-
private static final InetSocketAddress HTTP_SERVER_SOCKET_ADDR = new InetSocketAddress(LOCALHOST, 8080);
40+
private static final String URL_FORMAT = "http://%s:%d";
4441

4542
@RegisterExtension
4643
public SshServerExtension fixture = new SshServerExtension();
@@ -49,21 +46,21 @@ public class RemotePortForwarderTest {
4946
public HttpServer httpServer = new HttpServer();
5047

5148
@BeforeEach
52-
public void setUp() throws IOException {
49+
public void setUp() {
5350
fixture.getServer().setForwardingFilter(new AcceptAllForwardingFilter());
54-
File file = Files.createFile(httpServer.getDocRoot().toPath().resolve("index.html")).toFile();
55-
FileUtil.writeToFile(file, "<html><head/><body><h1>Hi!</h1></body></html>");
5651
}
5752

5853
@Test
5954
public void shouldHaveWorkingHttpServer() throws IOException {
60-
assertEquals(200, httpGet(8080));
55+
final URI serverUrl = httpServer.getServerUrl();
56+
57+
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, httpGet(serverUrl.getHost(), serverUrl.getPort()));
6158
}
6259

6360
@Test
6461
public void shouldDynamicallyForwardPortForLocalhost() throws IOException {
6562
SSHClient sshClient = getFixtureClient();
66-
RemotePortForwarder.Forward bind = forwardPort(sshClient, "127.0.0.1", new SinglePort(0));
63+
RemotePortForwarder.Forward bind = forwardPort(sshClient, LOCALHOST, new SinglePort(0));
6764
assertHttpGetSuccess(bind);
6865
}
6966

@@ -84,7 +81,7 @@ public void shouldDynamicallyForwardPortForAllProtocols() throws IOException {
8481
@Test
8582
public void shouldForwardPortForLocalhost() throws IOException {
8683
SSHClient sshClient = getFixtureClient();
87-
RemotePortForwarder.Forward bind = forwardPort(sshClient, "127.0.0.1", RANGE);
84+
RemotePortForwarder.Forward bind = forwardPort(sshClient, LOCALHOST, RANGE);
8885
assertHttpGetSuccess(bind);
8986
}
9087

@@ -103,17 +100,22 @@ public void shouldForwardPortForAllProtocols() throws IOException {
103100
}
104101

105102
private void assertHttpGetSuccess(final RemotePortForwarder.Forward bind) throws IOException {
106-
assertEquals(200, httpGet(bind.getPort()));
103+
final String bindAddress = bind.getAddress();
104+
final String address = bindAddress.isEmpty() ? LOCALHOST : bindAddress;
105+
final int port = bind.getPort();
106+
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, httpGet(address, port));
107107
}
108108

109109
private RemotePortForwarder.Forward forwardPort(SSHClient sshClient, String address, PortRange portRange) throws IOException {
110110
while (true) {
111+
final URI serverUrl = httpServer.getServerUrl();
112+
final InetSocketAddress serverAddress = new InetSocketAddress(serverUrl.getHost(), serverUrl.getPort());
111113
try {
112114
return sshClient.getRemotePortForwarder().bind(
113115
// where the server should listen
114116
new RemotePortForwarder.Forward(address, portRange.nextPort()),
115117
// what we do with incoming connections that are forwarded to us
116-
new SocketForwardingConnectListener(HTTP_SERVER_SOCKET_ADDR));
118+
new SocketForwardingConnectListener(serverAddress));
117119
} catch (ConnectionException ce) {
118120
if (!portRange.hasNext()) {
119121
throw ce;
@@ -122,8 +124,8 @@ private RemotePortForwarder.Forward forwardPort(SSHClient sshClient, String addr
122124
}
123125
}
124126

125-
private int httpGet(final int port) throws IOException {
126-
final URL url = new URL(String.format(LOCALHOST_URL_FORMAT, port));
127+
private int httpGet(final String address, final int port) throws IOException {
128+
final URL url = new URL(String.format(URL_FORMAT, address, port));
127129
final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
128130
urlConnection.setConnectTimeout(3000);
129131
urlConnection.setRequestMethod("GET");

src/test/java/com/hierynomus/sshj/test/HttpServer.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,36 @@
1919
import org.junit.jupiter.api.extension.BeforeEachCallback;
2020
import org.junit.jupiter.api.extension.ExtensionContext;
2121

22-
import java.io.File;
23-
import java.nio.file.Files;
22+
import java.net.InetSocketAddress;
23+
import java.net.URI;
2424

2525
/**
2626
* Can be used to setup a test HTTP server
2727
*/
2828
public class HttpServer implements BeforeEachCallback, AfterEachCallback {
2929

30-
private org.glassfish.grizzly.http.server.HttpServer httpServer;
30+
private static final String BIND_ADDRESS = "127.0.0.1";
3131

32-
33-
private File docRoot ;
32+
private com.sun.net.httpserver.HttpServer httpServer;
3433

3534
@Override
36-
public void afterEach(ExtensionContext context) throws Exception {
37-
try {
38-
httpServer.shutdownNow();
39-
} catch (Exception e) {}
35+
public void afterEach(ExtensionContext context) {
4036
try {
41-
docRoot.delete();
42-
} catch (Exception e) {}
43-
37+
httpServer.stop(0);
38+
} catch (Exception ignored) {}
4439
}
4540

4641
@Override
4742
public void beforeEach(ExtensionContext context) throws Exception {
48-
docRoot = Files.createTempDirectory("sshj").toFile();
49-
httpServer = org.glassfish.grizzly.http.server.HttpServer.createSimpleServer(docRoot.getAbsolutePath());
43+
httpServer = com.sun.net.httpserver.HttpServer.create();
44+
final InetSocketAddress socketAddress = new InetSocketAddress(BIND_ADDRESS, 0);
45+
httpServer.bind(socketAddress, 10);
5046
httpServer.start();
5147
}
5248

53-
public org.glassfish.grizzly.http.server.HttpServer getHttpServer() {
54-
return httpServer;
55-
}
56-
57-
public File getDocRoot() {
58-
return docRoot;
49+
public URI getServerUrl() {
50+
final InetSocketAddress bindAddress = httpServer.getAddress();
51+
final String serverUrl = String.format("http://%s:%d", BIND_ADDRESS, bindAddress.getPort());
52+
return URI.create(serverUrl);
5953
}
6054
}

0 commit comments

Comments
 (0)