aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java
diff options
context:
space:
mode:
authorJarno Elonen <elonen@iki.fi>2015-09-13 20:49:59 +0300
committerJarno Elonen <elonen@iki.fi>2015-09-13 20:49:59 +0300
commit60b7e9249246749a16bf8bc29aa117e4201eae8b (patch)
tree35c559493a5ec0e92846c51e048f683fdd5c4057 /core/src/main/java
parent9f38ca21e99986290bf65a8fec17d2592d98d744 (diff)
parentf3b51b6d9e8b24f2f2e021465b0f5972ad12892d (diff)
downloadnanohttpd-60b7e9249246749a16bf8bc29aa117e4201eae8b.tar.gz
Merge branch 'master' into static-new-xx
Diffstat (limited to 'core/src/main/java')
-rw-r--r--core/src/main/java/fi/iki/elonen/NanoHTTPD.java44
-rw-r--r--core/src/main/java/fi/iki/elonen/util/ServerRunner.java75
2 files changed, 101 insertions, 18 deletions
diff --git a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
index 689ef7c..da87bbb 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;
@@ -457,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;
@@ -902,8 +924,6 @@ public abstract class NanoHTTPD {
@Override
public void parseBody(Map<String, String> files) throws IOException, ResponseException {
- final int REQUEST_BUFFER_LEN = 512;
- final int MEMORY_STORE_LIMIT = 1024;
RandomAccessFile randomAccessFile = null;
try {
long size = getBodySize();
@@ -1576,17 +1596,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 +1608,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) {
@@ -1634,7 +1642,7 @@ public abstract class NanoHTTPD {
private final int myPort;
- private ServerSocket myServerSocket;
+ private volatile ServerSocket myServerSocket;
private SSLServerSocketFactory sslServerSocketFactory;
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..e0aa3db
--- /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(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
+ } 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 <T extends NanoHTTPD> void run(Class<T> serverClass) {
+ try {
+ executeInstance(serverClass.newInstance());
+ } catch (Exception e) {
+ ServerRunner.LOG.log(Level.SEVERE, "Cound nor create server", e);
+ }
+ }
+}