From c94ba3af36f433e41676b9c571d063a938e6a290 Mon Sep 17 00:00:00 2001 From: Victor Nikiforov Date: Mon, 24 Aug 2015 13:36:24 +0300 Subject: Issue #214 - NanoHTTPD uri router. Fix pom versions --- core/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/pom.xml b/core/pom.xml index 24e36a3..1ddc30d 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -3,7 +3,7 @@ org.nanohttpd nanohttpd-project - 2.2.0-SNAPSHOT + 2.2.1-SNAPSHOT nanohttpd jar -- cgit v1.2.3 From 4344efd565b3ad079eff4780619a964c86ac0b11 Mon Sep 17 00:00:00 2001 From: Jarno Elonen Date: Fri, 4 Sep 2015 18:18:21 +0300 Subject: Fixed a potential CPU cache issue. Closes #229. --- core/src/main/java/fi/iki/elonen/NanoHTTPD.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java index 32e6e87..7992650 100644 --- a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java +++ b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java @@ -1634,7 +1634,7 @@ public abstract class NanoHTTPD { private final int myPort; - private ServerSocket myServerSocket; + private volatile ServerSocket myServerSocket; private SSLServerSocketFactory sslServerSocketFactory; -- cgit v1.2.3 From 6c4d9f5a060c8621cfd1cb3c9132d2bfaecaabd3 Mon Sep 17 00:00:00 2001 From: ritchie Date: Fri, 11 Sep 2015 07:52:45 +0200 Subject: first refactorings for own module nanolets #223 --- core/pom.xml | 2 +- .../main/java/fi/iki/elonen/util/ServerRunner.java | 75 ++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/fi/iki/elonen/util/ServerRunner.java (limited to 'core') diff --git a/core/pom.xml b/core/pom.xml index 1ddc30d..24e36a3 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -3,7 +3,7 @@ org.nanohttpd nanohttpd-project - 2.2.1-SNAPSHOT + 2.2.0-SNAPSHOT nanohttpd jar diff --git a/core/src/main/java/fi/iki/elonen/util/ServerRunner.java b/core/src/main/java/fi/iki/elonen/util/ServerRunner.java new file mode 100644 index 0000000..c8d22d4 --- /dev/null +++ b/core/src/main/java/fi/iki/elonen/util/ServerRunner.java @@ -0,0 +1,75 @@ +package fi.iki.elonen.util; + +/* + * #%L + * NanoHttpd-Webserver + * %% + * Copyright (C) 2012 - 2015 nanohttpd + * %% + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the nanohttpd nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * #L% + */ + +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; + +import fi.iki.elonen.NanoHTTPD; + +public class ServerRunner { + + /** + * logger to log to. + */ + private static final Logger LOG = Logger.getLogger(ServerRunner.class.getName()); + + public static void executeInstance(NanoHTTPD server) { + try { + server.start(); + } catch (IOException ioe) { + System.err.println("Couldn't start server:\n" + ioe); + System.exit(-1); + } + + System.out.println("Server started, Hit Enter to stop.\n"); + + try { + System.in.read(); + } catch (Throwable ignored) { + } + + server.stop(); + System.out.println("Server stopped.\n"); + } + + public static void run(Class serverClass) { + try { + executeInstance(serverClass.newInstance()); + } catch (Exception e) { + ServerRunner.LOG.log(Level.SEVERE, "Cound nor create server", e); + } + } +} -- cgit v1.2.3 From ffabe5fa1bbf87cffbfbaaa3ed255fa23d273535 Mon Sep 17 00:00:00 2001 From: ritchie Date: Sun, 13 Sep 2015 06:22:54 +0200 Subject: file upload refactorings and set minimal coverage per module #219 #216 --- core/pom.xml | 3 +++ 1 file changed, 3 insertions(+) (limited to 'core') diff --git a/core/pom.xml b/core/pom.xml index 24e36a3..2f8f36e 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -22,4 +22,7 @@ test + + 0.77 + -- cgit v1.2.3 From 3ea79cfc3ef8447430e6fcc9570c1551831ff9c4 Mon Sep 17 00:00:00 2001 From: ritchie Date: Sun, 13 Sep 2015 09:18:09 +0200 Subject: unit tests for ssl support #197 --- core/pom.xml | 2 +- core/src/main/java/fi/iki/elonen/NanoHTTPD.java | 36 +++++---- .../java/fi/iki/elonen/HttpHeadRequestTest.java | 2 +- .../test/java/fi/iki/elonen/HttpSSLServerTest.java | 87 +++++++++++++++++++++ .../test/java/fi/iki/elonen/HttpServerTest.java | 8 +- .../java/fi/iki/elonen/HttpSessionHeadersTest.java | 5 -- core/src/test/resources/keystore.jks | Bin 0 -> 2276 bytes 7 files changed, 116 insertions(+), 24 deletions(-) create mode 100644 core/src/test/java/fi/iki/elonen/HttpSSLServerTest.java create mode 100644 core/src/test/resources/keystore.jks (limited to 'core') diff --git a/core/pom.xml b/core/pom.xml index 2f8f36e..b742250 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -23,6 +23,6 @@ - 0.77 + 0.81 diff --git a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java index d75c762..14e5299 100644 --- a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java +++ b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java @@ -33,7 +33,25 @@ package fi.iki.elonen; * #L% */ -import java.io.*; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.Closeable; +import java.io.DataOutput; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.PushbackInputStream; +import java.io.RandomAccessFile; +import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.ServerSocket; @@ -1576,17 +1594,11 @@ public abstract class NanoHTTPD { * by the caller. */ public static SSLServerSocketFactory makeSSLSocketFactory(KeyStore loadedKeyStore, KeyManagerFactory loadedKeyFactory) throws IOException { - SSLServerSocketFactory res = null; try { - TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); - trustManagerFactory.init(loadedKeyStore); - SSLContext ctx = SSLContext.getInstance("TLS"); - ctx.init(loadedKeyFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null); - res = ctx.getServerSocketFactory(); + return makeSSLSocketFactory(loadedKeyStore, loadedKeyFactory.getKeyManagers()); } catch (Exception e) { throw new IOException(e.getMessage()); } - return res; } /** @@ -1594,22 +1606,16 @@ public abstract class NanoHTTPD { * certificate and passphrase */ public static SSLServerSocketFactory makeSSLSocketFactory(String keyAndTrustStoreClasspathPath, char[] passphrase) throws IOException { - SSLServerSocketFactory res = null; try { KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); InputStream keystoreStream = NanoHTTPD.class.getResourceAsStream(keyAndTrustStoreClasspathPath); keystore.load(keystoreStream, passphrase); - TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); - trustManagerFactory.init(keystore); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keystore, passphrase); - SSLContext ctx = SSLContext.getInstance("TLS"); - ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null); - res = ctx.getServerSocketFactory(); + return makeSSLSocketFactory(keystore, keyManagerFactory); } catch (Exception e) { throw new IOException(e.getMessage()); } - return res; } private static final void safeClose(Object closeable) { diff --git a/core/src/test/java/fi/iki/elonen/HttpHeadRequestTest.java b/core/src/test/java/fi/iki/elonen/HttpHeadRequestTest.java index 4eb3147..9b5983e 100644 --- a/core/src/test/java/fi/iki/elonen/HttpHeadRequestTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpHeadRequestTest.java @@ -45,7 +45,7 @@ import org.junit.Test; public class HttpHeadRequestTest extends HttpServerTest { @Override - public void setUp() { + public void setUp() throws Exception { super.setUp(); String responseBody = "Success!"; this.testServer.response = NanoHTTPD.newFixedLengthResponse(responseBody); diff --git a/core/src/test/java/fi/iki/elonen/HttpSSLServerTest.java b/core/src/test/java/fi/iki/elonen/HttpSSLServerTest.java new file mode 100644 index 0000000..4923cca --- /dev/null +++ b/core/src/test/java/fi/iki/elonen/HttpSSLServerTest.java @@ -0,0 +1,87 @@ +package fi.iki.elonen; + +/* + * #%L + * NanoHttpd-Core + * %% + * Copyright (C) 2012 - 2015 nanohttpd + * %% + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the nanohttpd nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * #L% + */ + +import java.io.File; +import java.io.IOException; + +import javax.net.ssl.SSLServerSocketFactory; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpTrace; +import org.apache.http.impl.client.DefaultHttpClient; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class HttpSSLServerTest extends HttpServerTest { + + @Test + public void testSSLConnection() throws ClientProtocolException, IOException { + DefaultHttpClient httpclient = new DefaultHttpClient(); + HttpTrace httphead = new HttpTrace("https://localhost:9043/index.html"); + HttpResponse response = httpclient.execute(httphead); + HttpEntity entity = response.getEntity(); + Assert.assertEquals(200, response.getStatusLine().getStatusCode()); + } + + @Before + public void setUp() throws Exception { + System.setProperty("javax.net.ssl.trustStore", new File("src/test/resources/keystore.jks").getAbsolutePath()); + this.testServer = new TestServer(9043); + this.testServer.makeSecure(NanoHTTPD.makeSSLSocketFactory("/keystore.jks", "password".toCharArray())); + this.tempFileManager = new TestTempFileManager(); + this.testServer.start(); + try { + long start = System.currentTimeMillis(); + Thread.sleep(100L); + while (!this.testServer.wasStarted()) { + Thread.sleep(100L); + if (System.currentTimeMillis() - start > 2000) { + Assert.fail("could not start server"); + } + } + } catch (InterruptedException e) { + } + } + + @After + public void tearDown() { + this.testServer.stop(); + } +} diff --git a/core/src/test/java/fi/iki/elonen/HttpServerTest.java b/core/src/test/java/fi/iki/elonen/HttpServerTest.java index a8215f7..742942d 100644 --- a/core/src/test/java/fi/iki/elonen/HttpServerTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpServerTest.java @@ -83,6 +83,10 @@ public class HttpServerTest { super(8192); } + public TestServer(int port) { + super(port); + } + public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream) { return new HTTPSession(tempFileManager, inputStream, outputStream); } @@ -131,7 +135,7 @@ public class HttpServerTest { protected TestServer testServer; - private TestTempFileManager tempFileManager; + protected TestTempFileManager tempFileManager; protected void assertLinesOfText(String[] expected, List lines) { // assertEquals(expected.length, lines.size()); @@ -177,7 +181,7 @@ public class HttpServerTest { } @Before - public void setUp() { + public void setUp() throws Exception { this.testServer = new TestServer(); this.tempFileManager = new TestTempFileManager(); } diff --git a/core/src/test/java/fi/iki/elonen/HttpSessionHeadersTest.java b/core/src/test/java/fi/iki/elonen/HttpSessionHeadersTest.java index 65947b9..1494e28 100644 --- a/core/src/test/java/fi/iki/elonen/HttpSessionHeadersTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpSessionHeadersTest.java @@ -48,11 +48,6 @@ public class HttpSessionHeadersTest extends HttpServerTest { private static final TestTempFileManager TEST_TEMP_FILE_MANAGER = new TestTempFileManager(); - @Override - public void setUp() { - super.setUp(); - } - @Test @Ignore public void testHeadersRemoteIp() throws Exception { diff --git a/core/src/test/resources/keystore.jks b/core/src/test/resources/keystore.jks new file mode 100644 index 0000000..354b5d3 Binary files /dev/null and b/core/src/test/resources/keystore.jks differ -- cgit v1.2.3 From 4cfc55d35e82a603c677d61abdd56aca862ec148 Mon Sep 17 00:00:00 2001 From: ritchie Date: Sun, 13 Sep 2015 16:00:16 +0200 Subject: small code style correction --- core/src/main/java/fi/iki/elonen/NanoHTTPD.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java index 14e5299..45b036b 100644 --- a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java +++ b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java @@ -475,6 +475,10 @@ public abstract class NanoHTTPD { protected class HTTPSession implements IHTTPSession { + private static final int REQUEST_BUFFER_LEN = 512; + + private static final int MEMORY_STORE_LIMIT = 1024; + public static final int BUFSIZE = 8192; private final TempFileManager tempFileManager; @@ -920,8 +924,6 @@ public abstract class NanoHTTPD { @Override public void parseBody(Map files) throws IOException, ResponseException { - final int REQUEST_BUFFER_LEN = 512; - final int MEMORY_STORE_LIMIT = 1024; RandomAccessFile randomAccessFile = null; try { long size = getBodySize(); -- cgit v1.2.3 From f3b51b6d9e8b24f2f2e021465b0f5972ad12892d Mon Sep 17 00:00:00 2001 From: Jarno Elonen Date: Sun, 13 Sep 2015 20:21:56 +0300 Subject: Changed ServerRunner to daemon=false. Closes #224 --- core/src/main/java/fi/iki/elonen/util/ServerRunner.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/main/java/fi/iki/elonen/util/ServerRunner.java b/core/src/main/java/fi/iki/elonen/util/ServerRunner.java index c8d22d4..e0aa3db 100644 --- a/core/src/main/java/fi/iki/elonen/util/ServerRunner.java +++ b/core/src/main/java/fi/iki/elonen/util/ServerRunner.java @@ -48,7 +48,7 @@ public class ServerRunner { public static void executeInstance(NanoHTTPD server) { try { - server.start(); + server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false); } catch (IOException ioe) { System.err.println("Couldn't start server:\n" + ioe); System.exit(-1); -- cgit v1.2.3