From 709bd63d3d1d89e4bb35a2f721d3b2d07cec892d Mon Sep 17 00:00:00 2001 From: Matthieu Brouillard Date: Wed, 8 Jul 2015 14:02:12 +0200 Subject: fixes #25 add basic CORS support --- README.md | 4 + .../main/java/fi/iki/elonen/SimpleWebServer.java | 65 ++++++++- .../java/fi/iki/elonen/AbstractTestHttpServer.java | 67 +++++++++ .../java/fi/iki/elonen/TestCorsHttpServer.java | 154 +++++++++++++++++++++ .../test/java/fi/iki/elonen/TestHttpServer.java | 25 +--- 5 files changed, 284 insertions(+), 31 deletions(-) create mode 100644 webserver/src/test/java/fi/iki/elonen/AbstractTestHttpServer.java create mode 100644 webserver/src/test/java/fi/iki/elonen/TestCorsHttpServer.java diff --git a/README.md b/README.md index 6f7f439..5eb1084 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,10 @@ NanoHTTPD project currently consist of four parts: * File server serves also very long files without memory overhead. * Contains a built-in list of most common MIME types. * Runtime extension support (extensions that serve particular MIME types) - example extension that serves Markdown formatted files. Simply including an extension JAR in the webserver classpath is enough for the extension to be loaded. +* Simple [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) support via `--cors` + * by default serves `Access-Control-Allow-Headers: origin,accept,content-type` + * possibility to set `Access-Control-Allow-Headers` by setting System property: `AccessControlAllowHeader` + * _example: _ `-DAccessControlAllowHeader=origin,accept,content-type,Authorization` ## Maven dependencies diff --git a/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java b/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java index cd2e9bd..e966807 100644 --- a/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java +++ b/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java @@ -137,6 +137,7 @@ public class SimpleWebServer extends NanoHTTPD { String host = null; // bind to all interfaces by default List rootDirs = new ArrayList(); boolean quiet = false; + boolean cors = false; Map options = new HashMap(); // Parse command-line, with short and long versions of the options. @@ -149,6 +150,8 @@ public class SimpleWebServer extends NanoHTTPD { quiet = true; } else if (args[i].equalsIgnoreCase("-d") || args[i].equalsIgnoreCase("--dir")) { rootDirs.add(new File(args[i + 1]).getAbsoluteFile()); + } else if (args[i].equalsIgnoreCase("--cors")) { + cors = true; } else if (args[i].equalsIgnoreCase("--licence")) { System.out.println(SimpleWebServer.LICENCE + "\n"); } else if (args[i].startsWith("-X:")) { @@ -199,7 +202,7 @@ public class SimpleWebServer extends NanoHTTPD { } } - ServerRunner.executeInstance(new SimpleWebServer(host, port, rootDirs, quiet)); + ServerRunner.executeInstance(new SimpleWebServer(host, port, rootDirs, quiet, cors)); } protected static void registerPluginForMimeType(String[] indexFiles, String mimeType, WebServerPlugin plugin, Map commandLineOptions) { @@ -223,20 +226,26 @@ public class SimpleWebServer extends NanoHTTPD { private final boolean quiet; + private final boolean cors; + protected List rootDirs; - public SimpleWebServer(String host, int port, File wwwroot, boolean quiet) { - super(host, port); - this.quiet = quiet; - this.rootDirs = new ArrayList(); - this.rootDirs.add(wwwroot); + public SimpleWebServer(String host, int port, File wwwroot, boolean quiet, boolean useCORS) { + this(host, port, Collections.singletonList(wwwroot), quiet, useCORS); + } - init(); + public SimpleWebServer(String host, int port, File wwwroot, boolean quiet) { + this(host, port, Collections.singletonList(wwwroot), quiet, false); } public SimpleWebServer(String host, int port, List wwwroots, boolean quiet) { + this(host, port, wwwroots, quiet, false); + } + + public SimpleWebServer(String host, int port, List wwwroots, boolean quiet, boolean useCORS) { super(host, port); this.quiet = quiet; + this.cors = useCORS; this.rootDirs = new ArrayList(wwwroots); init(); @@ -394,6 +403,21 @@ public class SimpleWebServer extends NanoHTTPD { } private Response respond(Map headers, IHTTPSession session, String uri) { + // First let's handle CORS OPTION query + Response r; + if (cors && Method.OPTIONS.equals(session.getMethod())) { + r = new NanoHTTPD.Response(Response.Status.OK, MIME_PLAINTEXT, null, 0); + } else { + r = defaultRespond(headers, session, uri); + } + + if (cors) { + r = addCORSHeaders(headers, r); + } + return r; + } + + private Response defaultRespond(Map headers, IHTTPSession session, String uri) { // Remove URL arguments uri = uri.trim().replace(File.separatorChar, '/'); if (uri.indexOf('?') >= 0) { @@ -596,4 +620,31 @@ public class SimpleWebServer extends NanoHTTPD { res.addHeader("Accept-Ranges", "bytes"); return res; } + + private Response addCORSHeaders(Map queryHeaders, Response resp) { + resp.addHeader("Access-Control-Allow-Origin", "*"); + resp.addHeader("Access-Control-Allow-Headers", calculateAllowHeaders(queryHeaders)); + resp.addHeader("Access-Control-Allow-Credentials", "true"); + resp.addHeader("Access-Control-Allow-Methods", ALLOWED_METHODS); + resp.addHeader("Access-Control-Max-Age", "" + MAX_AGE); + + return resp; + } + + private String calculateAllowHeaders(Map queryHeaders) { + // here we should use the given asked headers + // but NanoHttpd uses a Map whereas it is possible for requester to send + // several time the same header + // let's just use default values for this version + return System.getProperty(ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME, DEFAULT_ALLOWED_HEADERS); + } + + private final static String ALLOWED_METHODS = "GET, POST, PUT, DELETE, OPTIONS, HEAD"; + + private final static int MAX_AGE = 42 * 60 * 60; + + // explicitly relax visibility to package for tests purposes + final static String DEFAULT_ALLOWED_HEADERS = "origin,accept,content-type"; + + public final static String ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME = "AccessControlAllowHeader"; } diff --git a/webserver/src/test/java/fi/iki/elonen/AbstractTestHttpServer.java b/webserver/src/test/java/fi/iki/elonen/AbstractTestHttpServer.java new file mode 100644 index 0000000..b56c2b0 --- /dev/null +++ b/webserver/src/test/java/fi/iki/elonen/AbstractTestHttpServer.java @@ -0,0 +1,67 @@ +package fi.iki.elonen; + +/* + * #%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.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import org.apache.http.HttpEntity; + +/** + * @author Matthieu Brouillard [matthieu@brouillard.fr] + */ +public class AbstractTestHttpServer { + + protected byte[] readContents(HttpEntity entity) throws IOException { + InputStream instream = entity.getContent(); + return readContents(instream); + } + + protected byte[] readContents(InputStream instream) throws IOException { + byte[] bytes; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try { + byte[] buffer = new byte[1024]; + int count; + while ((count = instream.read(buffer)) >= 0) { + out.write(buffer, 0, count); + } + bytes = out.toByteArray(); + } finally { + instream.close(); + } + return bytes; + } + +} diff --git a/webserver/src/test/java/fi/iki/elonen/TestCorsHttpServer.java b/webserver/src/test/java/fi/iki/elonen/TestCorsHttpServer.java new file mode 100644 index 0000000..93f4699 --- /dev/null +++ b/webserver/src/test/java/fi/iki/elonen/TestCorsHttpServer.java @@ -0,0 +1,154 @@ +package fi.iki.elonen; + +/* + * #%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.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; + +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpOptions; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author Matthieu Brouillard [matthieu@brouillard.fr] + */ +public class TestCorsHttpServer extends AbstractTestHttpServer { + + private static PipedOutputStream stdIn; + + private static Thread serverStartThread; + + @BeforeClass + public static void setUp() throws Exception { + stdIn = new PipedOutputStream(); + System.setIn(new PipedInputStream(stdIn)); + serverStartThread = new Thread(new Runnable() { + + @Override + public void run() { + String[] args = { + "--host", + "localhost", + "--port", + "9090", + "--dir", + "src/test/resources", + "--cors" + }; + SimpleWebServer.main(args); + } + }); + serverStartThread.start(); + // give the server some tine to start. + Thread.sleep(100); + } + + @AfterClass + public static void tearDown() throws Exception { + stdIn.write("\n\n".getBytes()); + serverStartThread.join(2000); + Assert.assertFalse(serverStartThread.isAlive()); + } + + @Test + public void doTestOption() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpOptions httpOption = new HttpOptions("http://localhost:9090/xxx/yyy.html"); + CloseableHttpResponse response = httpclient.execute(httpOption); + Assert.assertEquals(200, response.getStatusLine().getStatusCode()); + Assert.assertNotNull("Cors should have added a header: Access-Control-Allow-Origin", response.getLastHeader("Access-Control-Allow-Origin")); + Assert.assertEquals("Cors should have added a header: Access-Control-Allow-Origin: *", "*", response.getLastHeader("Access-Control-Allow-Origin").getValue()); + response.close(); + } + + @Test + public void doSomeBasicTest() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpget = new HttpGet("http://localhost:9090/testdir/test.html"); + CloseableHttpResponse response = httpclient.execute(httpget); + HttpEntity entity = response.getEntity(); + String string = new String(readContents(entity), "UTF-8"); + + Assert.assertNotNull("Cors should have added a header: Access-Control-Allow-Origin", response.getLastHeader("Access-Control-Allow-Origin")); + Assert.assertEquals("Cors should have added a header: Access-Control-Allow-Origin: *", "*", response.getLastHeader("Access-Control-Allow-Origin").getValue()); + Assert.assertEquals("\n\ndummy\n\n\n\t

it works

\n\n", string); + response.close(); + } + + @Test + public void testAccessControlAllowHeaderUsesDefaultsWithoutSystemProperty() throws Exception { + Assert.assertNull("no System " + SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME + " shoudl be set", + System.getProperty(SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME)); + + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpOptions httpOption = new HttpOptions("http://localhost:9090/xxx/yyy.html"); + CloseableHttpResponse response = httpclient.execute(httpOption); + Assert.assertEquals(200, response.getStatusLine().getStatusCode()); + Assert.assertEquals("Cors should have added a header: Access-Control-Allow-Headers: " + SimpleWebServer.DEFAULT_ALLOWED_HEADERS, + SimpleWebServer.DEFAULT_ALLOWED_HEADERS, response.getLastHeader("Access-Control-Allow-Headers").getValue()); + response.close(); + } + + @Test + public void testAccessControlAllowHeaderUsesSystemPropertyWhenSet() throws Exception { + Assert.assertNull("no System " + SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME + " shoudl be set", + System.getProperty(SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME)); + + final String expectedValue = "origin"; + System.setProperty(SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME, expectedValue); + + try { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpOptions httpOption = new HttpOptions("http://localhost:9090/xxx/yyy.html"); + CloseableHttpResponse response = httpclient.execute(httpOption); + Assert.assertEquals(200, response.getStatusLine().getStatusCode()); + Assert.assertEquals("Cors should have added a header: Access-Control-Allow-Headers: " + expectedValue, expectedValue, + response.getLastHeader("Access-Control-Allow-Headers").getValue()); + response.close(); + } finally { + System.clearProperty(SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME); + } + } +} diff --git a/webserver/src/test/java/fi/iki/elonen/TestHttpServer.java b/webserver/src/test/java/fi/iki/elonen/TestHttpServer.java index aa08f49..bb16741 100644 --- a/webserver/src/test/java/fi/iki/elonen/TestHttpServer.java +++ b/webserver/src/test/java/fi/iki/elonen/TestHttpServer.java @@ -50,7 +50,7 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -public class TestHttpServer { +public class TestHttpServer extends AbstractTestHttpServer { private static PipedOutputStream stdIn; @@ -146,27 +146,4 @@ public class TestHttpServer { response.close(); } - - private byte[] readContents(HttpEntity entity) throws IOException { - InputStream instream = entity.getContent(); - return readContents(instream); - } - - private byte[] readContents(InputStream instream) throws IOException { - byte[] bytes; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - - try { - byte[] buffer = new byte[1024]; - int count; - while ((count = instream.read(buffer)) >= 0) { - out.write(buffer, 0, count); - } - bytes = out.toByteArray(); - } finally { - instream.close(); - } - return bytes; - } - } -- cgit v1.2.3 From c5c77ab76ec0ca68687fb1ab500239085a4a0c6f Mon Sep 17 00:00:00 2001 From: Matthieu Brouillard Date: Mon, 20 Jul 2015 08:29:48 +0200 Subject: open visibility of SimpleWebServer#addCORSHeaders() method as requested in #207 comments --- webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java b/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java index e966807..2da13a1 100644 --- a/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java +++ b/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java @@ -621,7 +621,7 @@ public class SimpleWebServer extends NanoHTTPD { return res; } - private Response addCORSHeaders(Map queryHeaders, Response resp) { + protected Response addCORSHeaders(Map queryHeaders, Response resp) { resp.addHeader("Access-Control-Allow-Origin", "*"); resp.addHeader("Access-Control-Allow-Headers", calculateAllowHeaders(queryHeaders)); resp.addHeader("Access-Control-Allow-Credentials", "true"); -- cgit v1.2.3 From 42c25b82a0ef4d7eb3a5d69d325cda11f9803cf1 Mon Sep 17 00:00:00 2001 From: Victor Nikiforov Date: Wed, 22 Jul 2015 14:02:02 +0300 Subject: Add simple router example. Issue #214 - NanoHTTPD uri router --- .../src/main/java/fi/iki/elonen/router/App.java | 86 ++++++++++ .../java/fi/iki/elonen/router/DefaultHandler.java | 78 +++++++++ .../java/fi/iki/elonen/router/GeneralHandler.java | 82 ++++++++++ .../java/fi/iki/elonen/router/RouterNanoHTTPD.java | 145 ++++++++++++++++ .../java/fi/iki/elonen/router/RouterResponse.java | 80 +++++++++ .../java/fi/iki/elonen/router/StringUtils.java | 54 ++++++ .../main/java/fi/iki/elonen/router/UriPart.java | 63 +++++++ .../java/fi/iki/elonen/router/UriResource.java | 149 +++++++++++++++++ .../java/fi/iki/elonen/router/UriResponder.java | 50 ++++++ .../main/java/fi/iki/elonen/router/UriRouter.java | 182 +++++++++++++++++++++ .../java/fi/iki/elonen/router/UserHandler.java | 88 ++++++++++ 11 files changed, 1057 insertions(+) create mode 100644 samples/src/main/java/fi/iki/elonen/router/App.java create mode 100644 samples/src/main/java/fi/iki/elonen/router/DefaultHandler.java create mode 100644 samples/src/main/java/fi/iki/elonen/router/GeneralHandler.java create mode 100644 samples/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java create mode 100644 samples/src/main/java/fi/iki/elonen/router/RouterResponse.java create mode 100644 samples/src/main/java/fi/iki/elonen/router/StringUtils.java create mode 100644 samples/src/main/java/fi/iki/elonen/router/UriPart.java create mode 100644 samples/src/main/java/fi/iki/elonen/router/UriResource.java create mode 100644 samples/src/main/java/fi/iki/elonen/router/UriResponder.java create mode 100644 samples/src/main/java/fi/iki/elonen/router/UriRouter.java create mode 100644 samples/src/main/java/fi/iki/elonen/router/UserHandler.java diff --git a/samples/src/main/java/fi/iki/elonen/router/App.java b/samples/src/main/java/fi/iki/elonen/router/App.java new file mode 100644 index 0000000..e43e025 --- /dev/null +++ b/samples/src/main/java/fi/iki/elonen/router/App.java @@ -0,0 +1,86 @@ +package fi.iki.elonen.router; +/* + * #%L + * NanoHttpd-Samples + * %% + * 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% + */ + +/** + * Created by vnnv on 7/17/15. + * Simple httpd server based on NanoHTTPD + * Read the source. Everything is there. + */ + +import fi.iki.elonen.ServerRunner; + +import java.io.IOException; + +public class App extends RouterNanoHTTPD { + + private static final int PORT = 8081; + + /** + Create the server instance + */ + public App() throws IOException { + super(PORT); + addMappings(); + System.out.println("\nRunning! Point your browers to http://localhost:" + PORT + "/ \n"); + } + + /** + * Add the routes + * Every route is an absolute path + * Parameters starts with ":" + * Handler class should implement @UriResponder interface + * If the handler not implement UriResponder interface - toString() is used + */ + @Override + public void addMappings() { + super.addMappings(); + addRoute("/user", UserHandler.class); + addRoute("/user/:id", UserHandler.class); + addRoute("/user/help", GeneralHandler.class); + addRoute("/photos/:customer_id/:photo_id", null); + addRoute("/test", String.class); + } + + /** + * Main entry point + * @param args + */ + public static void main(String[] args) { + try { + ServerRunner.run(App.class); + } catch (Exception ioe) { + System.err.println("Couldn't start server:\n" + ioe); + } + } +} \ No newline at end of file diff --git a/samples/src/main/java/fi/iki/elonen/router/DefaultHandler.java b/samples/src/main/java/fi/iki/elonen/router/DefaultHandler.java new file mode 100644 index 0000000..57e7b47 --- /dev/null +++ b/samples/src/main/java/fi/iki/elonen/router/DefaultHandler.java @@ -0,0 +1,78 @@ +package fi.iki.elonen.router; +/* + * #%L + * NanoHttpd-Samples + * %% + * 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 fi.iki.elonen.NanoHTTPD; + +import java.io.ByteArrayInputStream; +import java.util.Map; + +/** + * Created by vnnv on 7/21/15. + * + */ +public abstract class DefaultHandler implements UriResponder { + + public abstract String getText(); + public abstract String getMimeType(); + public abstract NanoHTTPD.Response.IStatus getStatus(); + + public RouterResponse get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + String text = getText(); + ByteArrayInputStream inp = new ByteArrayInputStream(text.getBytes()); + + RouterResponse result = new RouterResponse(); + result.setData(inp); + result.setMimeType(getMimeType()); + result.setSize(text.getBytes().length); + result.setStatus(getStatus()); + + return result; + } + + public RouterResponse post(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + return get(uriResource, urlParams, session); + } + + public RouterResponse put(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + return get(uriResource, urlParams, session); + } + + public RouterResponse delete(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + return get(uriResource, urlParams, session); + } + public RouterResponse other(String method, UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + return get(uriResource, urlParams, session); + } + +} diff --git a/samples/src/main/java/fi/iki/elonen/router/GeneralHandler.java b/samples/src/main/java/fi/iki/elonen/router/GeneralHandler.java new file mode 100644 index 0000000..4e7f56a --- /dev/null +++ b/samples/src/main/java/fi/iki/elonen/router/GeneralHandler.java @@ -0,0 +1,82 @@ +package fi.iki.elonen.router; +/* + * #%L + * NanoHttpd-Samples + * %% + * 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 fi.iki.elonen.NanoHTTPD; + +import java.io.ByteArrayInputStream; +import java.util.Map; + +/** + * Created by vnnv on 7/22/15. + */ +public class GeneralHandler extends DefaultHandler { + + @Override + public String getText() { + return null; + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public NanoHTTPD.Response.IStatus getStatus() { + return NanoHTTPD.Response.Status.OK; + } + + public RouterResponse get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + String text = ""; + text += "

Url: " + session.getUri() + "


"; + Map queryParams = session.getParms(); + if (queryParams.size() > 0) { + for (Map.Entry entry : queryParams.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + text += "

Param '" + key + "' = " + value + "

"; + } + }else{ + text +="

no params in url


"; + } + + RouterResponse res = new RouterResponse(); + res.setData(new ByteArrayInputStream(text.getBytes())); + res.setSize(text.getBytes().length); + res.setStatus(getStatus()); + res.setMimeType(getMimeType()); + return res; + } + +} diff --git a/samples/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java b/samples/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java new file mode 100644 index 0000000..5da177c --- /dev/null +++ b/samples/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java @@ -0,0 +1,145 @@ +package fi.iki.elonen.router; +/* + * #%L + * NanoHttpd-Samples + * %% + * 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 fi.iki.elonen.NanoHTTPD; + +import java.util.Map; + +/** + * Created by vnnv on 7/21/15. + */ +public class RouterNanoHTTPD extends NanoHTTPD { + + /** + * Handling error 404 - unrecognized urls + */ + public static class Error404UriHandler extends DefaultHandler { + + public String getText() { + String res = "

Error 404: " + + "the requested page doesn't exist.

"; + return res; + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public Response.IStatus getStatus() { + return Response.Status.NOT_FOUND; + } + + } + + /** + * Handling index + */ + public static class IndexHandler extends DefaultHandler { + + public String getText() { + String res = "

Hello world!

"; + return res; + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public Response.IStatus getStatus() { + return Response.Status.OK; + } + + } + + public static class NotImplementedHandler extends DefaultHandler { + + public String getText() { + String res = "

The uri is mapped in the router, " + + "but no handler is specified.
" + + "Status: Not implemented!

"; + return res; + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public Response.IStatus getStatus() { + return Response.Status.OK; + } + + } + + + private UriRouter router; + + public RouterNanoHTTPD(int port) { + super(port); + router = new UriRouter(); + } + + public void addMappings() { + router.setNotImplemented(NotImplementedHandler.class); + router.setNotFoundHandler(Error404UriHandler.class); +// router.setNotFoundHandler(GeneralHandler.class); // You can use this instead of Error404UriHandler + router.addRoute("/", IndexHandler.class); + router.addRoute("/index.html", IndexHandler.class); + } + + public void addRoute(String url, Class handler) { + router.addRoute(url, handler); + } + + public void removeRoute(String url) { + router.removeRoute(url); + } + + @Override + public Response serve(IHTTPSession session) { + // Try to find match + UriResource uriResource = router.matchUrl(session.getUri()); + // Extract uri parameters + Map urlParams = router.getUrlParams(uriResource, session.getUri()); + // Process the uri + RouterResponse result = uriResource.process(urlParams, session); + // Return the response + return newFixedLengthResponse(result.getStatus(), result.getMimeType(), result.getData(), result.getSize()); + } +} diff --git a/samples/src/main/java/fi/iki/elonen/router/RouterResponse.java b/samples/src/main/java/fi/iki/elonen/router/RouterResponse.java new file mode 100644 index 0000000..ed16524 --- /dev/null +++ b/samples/src/main/java/fi/iki/elonen/router/RouterResponse.java @@ -0,0 +1,80 @@ +package fi.iki.elonen.router; +/* + * #%L + * NanoHttpd-Samples + * %% + * 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 fi.iki.elonen.NanoHTTPD; + +import java.io.InputStream; + +/** + * Created by vnnv on 7/21/15. + * Transport class because NanoHTTPD.Response has protected constructor + */ +public class RouterResponse { + private NanoHTTPD.Response.IStatus status; + private InputStream data; + private long size; + private String mimeType; + + public NanoHTTPD.Response.IStatus getStatus() { + return status; + } + + public void setStatus(NanoHTTPD.Response.IStatus status) { + this.status = status; + } + + public InputStream getData() { + return data; + } + + public void setData(InputStream data) { + this.data = data; + } + + public long getSize() { + return size; + } + + public void setSize(long size) { + this.size = size; + } + + public String getMimeType() { + return mimeType; + } + + public void setMimeType(String mimeType) { + this.mimeType = mimeType; + } +} diff --git a/samples/src/main/java/fi/iki/elonen/router/StringUtils.java b/samples/src/main/java/fi/iki/elonen/router/StringUtils.java new file mode 100644 index 0000000..bacaceb --- /dev/null +++ b/samples/src/main/java/fi/iki/elonen/router/StringUtils.java @@ -0,0 +1,54 @@ +package fi.iki.elonen.router; +/* + * #%L + * NanoHttpd-Samples + * %% + * 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% + */ + +/** + * Created by victor on 7/20/15. + * Util methods + */ +public class StringUtils { + public static String normalizeUri(String value) { + if (value == null) { + return value; + } + if (value.startsWith("/")) { + value = value.substring(1); + } + if (value.endsWith("/")) { + value = value.substring(0, value.length() - 1); + } + return value; + + } + +} diff --git a/samples/src/main/java/fi/iki/elonen/router/UriPart.java b/samples/src/main/java/fi/iki/elonen/router/UriPart.java new file mode 100644 index 0000000..19225b3 --- /dev/null +++ b/samples/src/main/java/fi/iki/elonen/router/UriPart.java @@ -0,0 +1,63 @@ +package fi.iki.elonen.router; +/* + * #%L + * NanoHttpd-Samples + * %% + * 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% + */ + +/** + * Created by vnnv on 7/20/15. + */ +public class UriPart { + private String name; + private boolean isParam; + + public UriPart(String name, boolean isParam) { + this.name = name; + this.isParam = isParam; + } + + @Override + public String toString() { + return "UriPart{" + + "name='" + name + '\'' + + ", isParam=" + isParam + + '}'; + } + + public boolean isParam() { + return isParam; + } + + public String getName() { + return name; + } + +} diff --git a/samples/src/main/java/fi/iki/elonen/router/UriResource.java b/samples/src/main/java/fi/iki/elonen/router/UriResource.java new file mode 100644 index 0000000..0ec45fe --- /dev/null +++ b/samples/src/main/java/fi/iki/elonen/router/UriResource.java @@ -0,0 +1,149 @@ +package fi.iki.elonen.router; +/* + * #%L + * NanoHttpd-Samples + * %% + * 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 fi.iki.elonen.NanoHTTPD; + +import java.io.ByteArrayInputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * Created by vnnv on 7/20/15. + * Represent single uri + */ +public class UriResource { + private boolean hasParameters; + private int uriParamsCount; + private String uri; + private List uriParts; + private Class handler; + + public UriResource(String uri, Class handler) { + this.hasParameters = false; + this.handler = handler; + uriParamsCount = 0; + if (uri != null) { + this.uri = StringUtils.normalizeUri(uri); + parse(); + } + } + + private void parse(){ + String[] parts = uri.split("/"); + uriParts = new ArrayList(); + for (String part : parts) { + boolean isParam = part.startsWith(":"); + UriPart uriPart = null; + if (isParam) { + hasParameters = true; + uriParamsCount++; + uriPart = new UriPart(part.substring(1), true); + }else{ + uriPart = new UriPart(part, false); + } + uriParts.add(uriPart); + } + + } + + public RouterResponse process(Map urlParams, NanoHTTPD.IHTTPSession session) { + String error = "General error!"; + if (handler != null) { + try { + Object object = handler.newInstance(); + if (object instanceof UriResponder) { + UriResponder responder = (UriResponder) object; + switch (session.getMethod()) { + case GET: return responder.get(this, urlParams, session); + case POST: return responder.post(this, urlParams, session); + case PUT: return responder.put(this, urlParams, session); + case DELETE: return responder.delete(this, urlParams, session); + default: return responder.other(session.getMethod().toString(), this, urlParams, session); + } + }else { + // return toString() + String text = "Return: " + handler.getCanonicalName() + ".toString() -> " + object.toString(); + RouterResponse res = new RouterResponse(); + res.setStatus(NanoHTTPD.Response.Status.OK); + res.setMimeType("text/plain"); + res.setData(new ByteArrayInputStream(text.getBytes())); + res.setSize(text.getBytes().length); + return res; + } + } catch (InstantiationException e) { + error = "Error: " + InstantiationException.class.getName() + " : " + e.getMessage(); + e.printStackTrace(); + } catch (IllegalAccessException e) { + error = "Error: " + IllegalAccessException.class.getName() + " : " + e.getMessage(); + e.printStackTrace(); + } + } + + RouterResponse res = new RouterResponse(); + res.setStatus(NanoHTTPD.Response.Status.INTERNAL_ERROR); + res.setMimeType("text/plain"); + res.setData(new ByteArrayInputStream(error.getBytes())); + res.setSize(error.getBytes().length); + return res; + } + + + @Override + public String toString() { + return "UrlResource{" + + "hasParameters=" + hasParameters + + ", uriParamsCount=" + uriParamsCount + + ", uri='" + (uri != null ? "/" : "") + uri + '\'' + + ", urlParts=" + uriParts + + '}'; + } + + public boolean hasParameters() { + return hasParameters; + } + + public String getUri() { + return uri; + } + + public List getUriParts() { + return uriParts; + } + + public int getUriParamsCount() { + return uriParamsCount; + } + +} diff --git a/samples/src/main/java/fi/iki/elonen/router/UriResponder.java b/samples/src/main/java/fi/iki/elonen/router/UriResponder.java new file mode 100644 index 0000000..1015250 --- /dev/null +++ b/samples/src/main/java/fi/iki/elonen/router/UriResponder.java @@ -0,0 +1,50 @@ +package fi.iki.elonen.router; +/* + * #%L + * NanoHttpd-Samples + * %% + * 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 fi.iki.elonen.NanoHTTPD; + +import java.util.Map; + +/** + * Created by vnnv on 7/20/15. + */ +public interface UriResponder { + + public RouterResponse get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); + public RouterResponse put(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); + public RouterResponse post(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); + public RouterResponse delete(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); + public RouterResponse other(String method, UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); + +} diff --git a/samples/src/main/java/fi/iki/elonen/router/UriRouter.java b/samples/src/main/java/fi/iki/elonen/router/UriRouter.java new file mode 100644 index 0000000..c28cecf --- /dev/null +++ b/samples/src/main/java/fi/iki/elonen/router/UriRouter.java @@ -0,0 +1,182 @@ +package fi.iki.elonen.router; +/* + * #%L + * NanoHttpd-Samples + * %% + * 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.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by vnnv on 7/17/15. + */ +public class UriRouter { + + private List mappings; + private UriResource error404Url; + private Class notImplemented; + + public UriRouter() { + mappings = new ArrayList(); + } + + /** + * Search in the mappings if the given url matches some of the rules + * If there are more than one marches returns the rule with less parameters + * e.g. + * mapping 1 = /user/:id + * mapping 2 = /user/help + * + * if the incoming uri is www.example.com/user/help - mapping 2 is returned + * if the incoming uri is www.example.com/user/3232 - mapping 1 is returned + * + * @param url + * @return + */ + public UriResource matchUrl(String url) { + + String work = StringUtils.normalizeUri(url); + + String[] parts = work.split("/"); + List resultList = new ArrayList(); + + for (UriResource u : mappings) { + + // Check if count of parts is the same + if (parts.length != u.getUriParts().size()) { + continue; // different + } + + List uriParts = u.getUriParts(); + + boolean match = true; + for (int i = 0; i < parts.length; i++) { + String currentPart = parts[i]; + UriPart uriPart = uriParts.get(i); + boolean goOn = false; + + if (currentPart.equals(uriPart.getName())) { + // exact match + goOn = true; + }else { + // not match + if (uriPart.isParam()){ + goOn = true; + }else{ + match = false; + goOn = false; + } + } + if (!goOn) { + match = false; + break; + } + } // for - iterate incoming url parts + if (match) { + resultList.add(u); // current match + } + } // end iterate over all rules + if (resultList.size() > 0) { + // some results + UriResource result = null; + if (resultList.size() > 1) { + //return the rule with less parameters + int params = 1024; + for (UriResource u : resultList) { + if (!u.hasParameters()) { + result = u; + break; + }else{ + if (u.getUriParamsCount() <= params) { + result = u; + } + } + } + return result; + }else{ + return resultList.get(0); + } + } + return error404Url; + } + + public void addRoute(String url, Class handler) { + if (url != null) { + if (handler != null) { + mappings.add(new UriResource(url, handler)); + }else{ + mappings.add(new UriResource(url, notImplemented)); + } + } + + } + + public void removeRoute(String url) { + if (mappings.contains(url)) { + mappings.remove(url); + } + } + + public void setNotFoundHandler(Class handler) { + error404Url = new UriResource(null, handler); + } + + public void setNotImplemented(Class handler) { + notImplemented = handler; + } + + /** + * Extract parameters and their values for the given route + * @param route + * @param uri + * @return + */ + public Map getUrlParams(UriResource route, String uri) { + Map result = new HashMap(); + if (route.getUri() == null) { + return result; + } + + String workUri = StringUtils.normalizeUri(uri); + String[] parts = workUri.split("/"); + + + for (int i = 0; i < parts.length; i++ ) { + UriPart currentPart = route.getUriParts().get(i); + if (currentPart.isParam()) { + result.put(currentPart.getName(), parts[i]); + } + } + return result; + } +} diff --git a/samples/src/main/java/fi/iki/elonen/router/UserHandler.java b/samples/src/main/java/fi/iki/elonen/router/UserHandler.java new file mode 100644 index 0000000..8999793 --- /dev/null +++ b/samples/src/main/java/fi/iki/elonen/router/UserHandler.java @@ -0,0 +1,88 @@ +package fi.iki.elonen.router; +/* + * #%L + * NanoHttpd-Samples + * %% + * 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 fi.iki.elonen.NanoHTTPD; + +import java.io.ByteArrayInputStream; +import java.util.Map; + + + +/** + * Created by vnnv on 7/21/15. + */ +public class UserHandler implements UriResponder { + public RouterResponse get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + String text = "User handler. Method: " + session.getMethod().toString() + "
"; + text += "

Uri parameters:

"; + for (Map.Entry entry : urlParams.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + text += "
Param: " + key + " Value: " + value + "
"; + } + text += "

Query parameters:

"; + for (Map.Entry entry : session.getParms().entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + text += "
Query Param: " + key + " Value: " + value + "
"; + } + text += ""; + + ByteArrayInputStream inp = new ByteArrayInputStream(text.getBytes()); + + RouterResponse result = new RouterResponse(); + result.setData(inp); + result.setMimeType("text/html"); + result.setSize(text.getBytes().length); + result.setStatus(NanoHTTPD.Response.Status.OK); + + return result; + } + + public RouterResponse post(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + return get(uriResource, urlParams, session); + } + + public RouterResponse put(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + return get(uriResource, urlParams, session); + } + + public RouterResponse delete(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + return get(uriResource, urlParams, session); + } + + public RouterResponse other(String method, UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + return get(uriResource, urlParams, session); + } +} -- cgit v1.2.3 From cbdd9d819348d2169d190f305b32d5240d1f60fc Mon Sep 17 00:00:00 2001 From: Matthieu Brouillard Date: Sun, 9 Aug 2015 12:27:05 +0200 Subject: enhance CORS support to allow to define origin --- README.md | 11 +- .../main/java/fi/iki/elonen/SimpleWebServer.java | 38 +++---- .../elonen/TestCorsHttpServerWithSingleOrigin.java | 121 +++++++++++++++++++++ 3 files changed, 150 insertions(+), 20 deletions(-) create mode 100644 webserver/src/test/java/fi/iki/elonen/TestCorsHttpServerWithSingleOrigin.java diff --git a/README.md b/README.md index 5eb1084..d2f224d 100644 --- a/README.md +++ b/README.md @@ -112,10 +112,19 @@ NanoHTTPD project currently consist of four parts: * File server serves also very long files without memory overhead. * Contains a built-in list of most common MIME types. * Runtime extension support (extensions that serve particular MIME types) - example extension that serves Markdown formatted files. Simply including an extension JAR in the webserver classpath is enough for the extension to be loaded. -* Simple [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) support via `--cors` +* Simple [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) support via `--cors` paramater * by default serves `Access-Control-Allow-Headers: origin,accept,content-type` * possibility to set `Access-Control-Allow-Headers` by setting System property: `AccessControlAllowHeader` * _example: _ `-DAccessControlAllowHeader=origin,accept,content-type,Authorization` + * possible values: + * `--cors`: activates CORS support, `Access-Control-Allow-Origin` will be set to `*` + * `--cors=some_value`: `Access-Control-Allow-Origin` will be set to `some_value`. + +**_CORS argument examples_** + + +* `--cors=http://appOne.company.com` +* `--cors="http://appOne.company.com, http://appTwo.company.com"`: note the double quotes so that the 2 URLs are considered part of a single argument. ## Maven dependencies diff --git a/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java b/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java index 2da13a1..2855b7b 100644 --- a/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java +++ b/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java @@ -32,7 +32,6 @@ package fi.iki.elonen; * OF THE POSSIBILITY OF SUCH DAMAGE. * #L% */ - import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -137,7 +136,7 @@ public class SimpleWebServer extends NanoHTTPD { String host = null; // bind to all interfaces by default List rootDirs = new ArrayList(); boolean quiet = false; - boolean cors = false; + String cors = null; Map options = new HashMap(); // Parse command-line, with short and long versions of the options. @@ -150,8 +149,12 @@ public class SimpleWebServer extends NanoHTTPD { quiet = true; } else if (args[i].equalsIgnoreCase("-d") || args[i].equalsIgnoreCase("--dir")) { rootDirs.add(new File(args[i + 1]).getAbsoluteFile()); - } else if (args[i].equalsIgnoreCase("--cors")) { - cors = true; + } else if (args[i].startsWith("--cors")) { + cors = "*"; + int equalIdx = args[i].indexOf('='); + if (equalIdx > 0) { + cors = args[i].substring(equalIdx + 1); + } } else if (args[i].equalsIgnoreCase("--licence")) { System.out.println(SimpleWebServer.LICENCE + "\n"); } else if (args[i].startsWith("-X:")) { @@ -167,7 +170,6 @@ public class SimpleWebServer extends NanoHTTPD { if (rootDirs.isEmpty()) { rootDirs.add(new File(".").getAbsoluteFile()); } - options.put("host", host); options.put("port", "" + port); options.put("quiet", String.valueOf(quiet)); @@ -182,7 +184,6 @@ public class SimpleWebServer extends NanoHTTPD { } } options.put("home", sb.toString()); - ServiceLoader serviceLoader = ServiceLoader.load(WebServerPluginInfo.class); for (WebServerPluginInfo info : serviceLoader) { String[] mimeTypes = info.getMimeTypes(); @@ -201,7 +202,6 @@ public class SimpleWebServer extends NanoHTTPD { registerPluginForMimeType(indexFiles, mime, info.getWebServerPlugin(mime), options); } } - ServerRunner.executeInstance(new SimpleWebServer(host, port, rootDirs, quiet, cors)); } @@ -226,26 +226,26 @@ public class SimpleWebServer extends NanoHTTPD { private final boolean quiet; - private final boolean cors; + private final String cors; protected List rootDirs; - public SimpleWebServer(String host, int port, File wwwroot, boolean quiet, boolean useCORS) { - this(host, port, Collections.singletonList(wwwroot), quiet, useCORS); + public SimpleWebServer(String host, int port, File wwwroot, boolean quiet, String cors) { + this(host, port, Collections.singletonList(wwwroot), quiet, cors); } public SimpleWebServer(String host, int port, File wwwroot, boolean quiet) { - this(host, port, Collections.singletonList(wwwroot), quiet, false); + this(host, port, Collections.singletonList(wwwroot), quiet, null); } public SimpleWebServer(String host, int port, List wwwroots, boolean quiet) { - this(host, port, wwwroots, quiet, false); + this(host, port, wwwroots, quiet, null); } - public SimpleWebServer(String host, int port, List wwwroots, boolean quiet, boolean useCORS) { + public SimpleWebServer(String host, int port, List wwwroots, boolean quiet, String cors) { super(host, port); this.quiet = quiet; - this.cors = useCORS; + this.cors = cors; this.rootDirs = new ArrayList(wwwroots); init(); @@ -405,14 +405,14 @@ public class SimpleWebServer extends NanoHTTPD { private Response respond(Map headers, IHTTPSession session, String uri) { // First let's handle CORS OPTION query Response r; - if (cors && Method.OPTIONS.equals(session.getMethod())) { + if (cors != null && Method.OPTIONS.equals(session.getMethod())) { r = new NanoHTTPD.Response(Response.Status.OK, MIME_PLAINTEXT, null, 0); } else { r = defaultRespond(headers, session, uri); } - if (cors) { - r = addCORSHeaders(headers, r); + if (cors != null) { + r = addCORSHeaders(headers, r, cors); } return r; } @@ -621,8 +621,8 @@ public class SimpleWebServer extends NanoHTTPD { return res; } - protected Response addCORSHeaders(Map queryHeaders, Response resp) { - resp.addHeader("Access-Control-Allow-Origin", "*"); + protected Response addCORSHeaders(Map queryHeaders, Response resp, String cors) { + resp.addHeader("Access-Control-Allow-Origin", cors); resp.addHeader("Access-Control-Allow-Headers", calculateAllowHeaders(queryHeaders)); resp.addHeader("Access-Control-Allow-Credentials", "true"); resp.addHeader("Access-Control-Allow-Methods", ALLOWED_METHODS); diff --git a/webserver/src/test/java/fi/iki/elonen/TestCorsHttpServerWithSingleOrigin.java b/webserver/src/test/java/fi/iki/elonen/TestCorsHttpServerWithSingleOrigin.java new file mode 100644 index 0000000..dbd2c4e --- /dev/null +++ b/webserver/src/test/java/fi/iki/elonen/TestCorsHttpServerWithSingleOrigin.java @@ -0,0 +1,121 @@ +package fi.iki.elonen; + +/* + * #%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.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; + +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpOptions; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author Matthieu Brouillard [matthieu@brouillard.fr] + */ +public class TestCorsHttpServerWithSingleOrigin extends AbstractTestHttpServer { + + private static PipedOutputStream stdIn; + + private static Thread serverStartThread; + + @BeforeClass + public static void setUp() throws Exception { + stdIn = new PipedOutputStream(); + System.setIn(new PipedInputStream(stdIn)); + serverStartThread = new Thread(new Runnable() { + + @Override + public void run() { + String[] args = { + "--host", + "localhost", + "--port", + "9090", + "--dir", + "src/test/resources", + "--cors=http://localhost:9090" + }; + SimpleWebServer.main(args); + } + }); + serverStartThread.start(); + // give the server some tine to start. + Thread.sleep(100); + } + + @AfterClass + public static void tearDown() throws Exception { + stdIn.write("\n\n".getBytes()); + serverStartThread.join(2000); + Assert.assertFalse(serverStartThread.isAlive()); + } + + @Test + public void doTestOption() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpOptions httpOption = new HttpOptions("http://localhost:9090/xxx/yyy.html"); + CloseableHttpResponse response = httpclient.execute(httpOption); + Assert.assertEquals(200, response.getStatusLine().getStatusCode()); + Assert.assertNotNull("Cors should have added a header: Access-Control-Allow-Origin", response.getLastHeader("Access-Control-Allow-Origin")); + Assert.assertEquals("Cors should have added a header: Access-Control-Allow-Origin: http://localhost:9090", "http://localhost:9090", + response.getLastHeader("Access-Control-Allow-Origin").getValue()); + response.close(); + } + + @Test + public void doSomeBasicTest() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpget = new HttpGet("http://localhost:9090/testdir/test.html"); + CloseableHttpResponse response = httpclient.execute(httpget); + HttpEntity entity = response.getEntity(); + String string = new String(readContents(entity), "UTF-8"); + + Assert.assertNotNull("Cors should have added a header: Access-Control-Allow-Origin", response.getLastHeader("Access-Control-Allow-Origin")); + Assert.assertEquals("Cors should have added a header: Access-Control-Allow-Origin: http://localhost:9090", "http://localhost:9090", + response.getLastHeader("Access-Control-Allow-Origin").getValue()); + Assert.assertEquals("\n\ndummy\n\n\n\t

it works

\n\n", string); + response.close(); + } +} -- cgit v1.2.3 From f22e738c41798cac1dc891a29a960f03a2e4336c Mon Sep 17 00:00:00 2001 From: Victor Nikiforov Date: Mon, 10 Aug 2015 11:46:31 +0300 Subject: Issue #216 - Integration with apache.commons.fileupload enhancement --- fileupload/pom.xml | 39 +++++++++++++++++ .../main/java/fi/iki/elonen/NanoFileUpload.java | 43 ++++++++++++++++++ .../main/java/fi/iki/elonen/NanoHttpdContext.java | 51 ++++++++++++++++++++++ pom.xml | 1 + 4 files changed, 134 insertions(+) create mode 100644 fileupload/pom.xml create mode 100644 fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java create mode 100644 fileupload/src/main/java/fi/iki/elonen/NanoHttpdContext.java diff --git a/fileupload/pom.xml b/fileupload/pom.xml new file mode 100644 index 0000000..da60ecf --- /dev/null +++ b/fileupload/pom.xml @@ -0,0 +1,39 @@ + + + + + nanohttpd-project + org.nanohttpd + 2.2.0-SNAPSHOT + + 4.0.0 + + apache-fileupload-integration + + + org.nanohttpd + nanohttpd + 2.2.0-SNAPSHOT + provided + + + + commons-fileupload + commons-fileupload + 1.3.1 + provided + + + + + javax.servlet + servlet-api + 2.5 + provided + + + + + \ No newline at end of file diff --git a/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java b/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java new file mode 100644 index 0000000..ec2e4e8 --- /dev/null +++ b/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java @@ -0,0 +1,43 @@ +package fi.iki.elonen; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileItemFactory; +import org.apache.commons.fileupload.FileItemIterator; +import org.apache.commons.fileupload.FileUpload; +import org.apache.commons.fileupload.FileUploadBase; +import org.apache.commons.fileupload.FileUploadException; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +/** + * Created by victor on 7/30/15. + */ +public class NanoFileUpload extends FileUpload { + private static final String POST_METHOD = "POST"; + + public static final boolean isMultipartContent(NanoHTTPD.IHTTPSession session) { + return !"POST".equalsIgnoreCase(session.getMethod().toString())?false: FileUploadBase.isMultipartContent(new NanoHttpdContext(session)); + } + + public NanoFileUpload() { + } + + public NanoFileUpload(FileItemFactory fileItemFactory) { + super(fileItemFactory); + } + + public List parseRequest(NanoHTTPD.IHTTPSession session) throws FileUploadException { + return this.parseRequest(new NanoHttpdContext(session)); + } + + public Map> parseParameterMap(NanoHTTPD.IHTTPSession session) throws FileUploadException { + return this.parseParameterMap(new NanoHttpdContext(session)); + } + + public FileItemIterator getItemIterator(NanoHTTPD.IHTTPSession session) throws FileUploadException, IOException { + return super.getItemIterator(new NanoHttpdContext(session)); + } + +} diff --git a/fileupload/src/main/java/fi/iki/elonen/NanoHttpdContext.java b/fileupload/src/main/java/fi/iki/elonen/NanoHttpdContext.java new file mode 100644 index 0000000..1c4d89f --- /dev/null +++ b/fileupload/src/main/java/fi/iki/elonen/NanoHttpdContext.java @@ -0,0 +1,51 @@ +package fi.iki.elonen; + +import org.apache.commons.fileupload.UploadContext; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Created by victor on 7/30/15. + */ +public class NanoHttpdContext implements UploadContext { + + private NanoHTTPD.IHTTPSession session; + + public NanoHttpdContext(NanoHTTPD.IHTTPSession session) { + this.session = session; + } + + @Override + public long contentLength() { + long size; + try { + String cl1 = session.getHeaders().get("content-length"); + size = Long.parseLong(cl1); + } catch (NumberFormatException var4) { + size = -1L; + } + + return size; + } + + @Override + public String getCharacterEncoding() { + return "UTF-8"; + } + + @Override + public String getContentType() { + return this.session.getHeaders().get("content-type"); + } + + @Override + public int getContentLength() { + return (int)contentLength(); + } + + @Override + public InputStream getInputStream() throws IOException { + return session.getInputStream(); + } +} diff --git a/pom.xml b/pom.xml index 67a8dc0..998f05a 100644 --- a/pom.xml +++ b/pom.xml @@ -88,6 +88,7 @@ webserver websocket markdown-plugin + fileupload -- cgit v1.2.3 From 4d2c4ad56fe28b0a96d90dfa3e6d4887988e8c00 Mon Sep 17 00:00:00 2001 From: Victor Nikiforov Date: Mon, 10 Aug 2015 12:06:14 +0300 Subject: Issue #216 - Integration with apache.commons.fileupload enhancement - Condensing the classes into a single class --- .../main/java/fi/iki/elonen/NanoFileUpload.java | 48 +++++++++++++++++++- .../main/java/fi/iki/elonen/NanoHttpdContext.java | 51 ---------------------- 2 files changed, 47 insertions(+), 52 deletions(-) delete mode 100644 fileupload/src/main/java/fi/iki/elonen/NanoHttpdContext.java diff --git a/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java b/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java index ec2e4e8..f51df3c 100644 --- a/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java +++ b/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java @@ -6,8 +6,10 @@ import org.apache.commons.fileupload.FileItemIterator; import org.apache.commons.fileupload.FileUpload; import org.apache.commons.fileupload.FileUploadBase; import org.apache.commons.fileupload.FileUploadException; +import org.apache.commons.fileupload.UploadContext; import java.io.IOException; +import java.io.InputStream; import java.util.List; import java.util.Map; @@ -15,10 +17,54 @@ import java.util.Map; * Created by victor on 7/30/15. */ public class NanoFileUpload extends FileUpload { + + public static class NanoHttpdContext implements UploadContext { + + private NanoHTTPD.IHTTPSession session; + + public NanoHttpdContext(NanoHTTPD.IHTTPSession session) { + this.session = session; + } + + @Override + public long contentLength() { + long size; + try { + String cl1 = session.getHeaders().get("content-length"); + size = Long.parseLong(cl1); + } catch (NumberFormatException var4) { + size = -1L; + } + + return size; + } + + @Override + public String getCharacterEncoding() { + return "UTF-8"; + } + + @Override + public String getContentType() { + return this.session.getHeaders().get("content-type"); + } + + @Override + public int getContentLength() { + return (int)contentLength(); + } + + @Override + public InputStream getInputStream() throws IOException { + return session.getInputStream(); + } + } + private static final String POST_METHOD = "POST"; public static final boolean isMultipartContent(NanoHTTPD.IHTTPSession session) { - return !"POST".equalsIgnoreCase(session.getMethod().toString())?false: FileUploadBase.isMultipartContent(new NanoHttpdContext(session)); + return !"POST".equalsIgnoreCase(session.getMethod().toString()) + ? false: FileUploadBase.isMultipartContent(new NanoHttpdContext(session)); } public NanoFileUpload() { diff --git a/fileupload/src/main/java/fi/iki/elonen/NanoHttpdContext.java b/fileupload/src/main/java/fi/iki/elonen/NanoHttpdContext.java deleted file mode 100644 index 1c4d89f..0000000 --- a/fileupload/src/main/java/fi/iki/elonen/NanoHttpdContext.java +++ /dev/null @@ -1,51 +0,0 @@ -package fi.iki.elonen; - -import org.apache.commons.fileupload.UploadContext; - -import java.io.IOException; -import java.io.InputStream; - -/** - * Created by victor on 7/30/15. - */ -public class NanoHttpdContext implements UploadContext { - - private NanoHTTPD.IHTTPSession session; - - public NanoHttpdContext(NanoHTTPD.IHTTPSession session) { - this.session = session; - } - - @Override - public long contentLength() { - long size; - try { - String cl1 = session.getHeaders().get("content-length"); - size = Long.parseLong(cl1); - } catch (NumberFormatException var4) { - size = -1L; - } - - return size; - } - - @Override - public String getCharacterEncoding() { - return "UTF-8"; - } - - @Override - public String getContentType() { - return this.session.getHeaders().get("content-type"); - } - - @Override - public int getContentLength() { - return (int)contentLength(); - } - - @Override - public InputStream getInputStream() throws IOException { - return session.getInputStream(); - } -} -- cgit v1.2.3 From eaf89cc0dabb96b443895f02625fd699ee740f5c Mon Sep 17 00:00:00 2001 From: Victor Nikiforov Date: Mon, 24 Aug 2015 13:35:25 +0300 Subject: Issue #214 - NanoHTTPD uri router. Single file --- .../src/main/java/fi/iki/elonen/router/App.java | 86 ---------- .../java/fi/iki/elonen/router/AppNanolets.java | 86 ++++++++++ .../java/fi/iki/elonen/router/DefaultHandler.java | 78 --------- .../java/fi/iki/elonen/router/GeneralHandler.java | 82 ---------- .../java/fi/iki/elonen/router/RouterResponse.java | 80 --------- .../java/fi/iki/elonen/router/StringUtils.java | 54 ------ .../main/java/fi/iki/elonen/router/UriPart.java | 63 ------- .../java/fi/iki/elonen/router/UriResource.java | 149 ----------------- .../java/fi/iki/elonen/router/UriResponder.java | 50 ------ .../main/java/fi/iki/elonen/router/UriRouter.java | 182 --------------------- .../java/fi/iki/elonen/router/UserHandler.java | 88 ---------- 11 files changed, 86 insertions(+), 912 deletions(-) delete mode 100644 samples/src/main/java/fi/iki/elonen/router/App.java create mode 100644 samples/src/main/java/fi/iki/elonen/router/AppNanolets.java delete mode 100644 samples/src/main/java/fi/iki/elonen/router/DefaultHandler.java delete mode 100644 samples/src/main/java/fi/iki/elonen/router/GeneralHandler.java delete mode 100644 samples/src/main/java/fi/iki/elonen/router/RouterResponse.java delete mode 100644 samples/src/main/java/fi/iki/elonen/router/StringUtils.java delete mode 100644 samples/src/main/java/fi/iki/elonen/router/UriPart.java delete mode 100644 samples/src/main/java/fi/iki/elonen/router/UriResource.java delete mode 100644 samples/src/main/java/fi/iki/elonen/router/UriResponder.java delete mode 100644 samples/src/main/java/fi/iki/elonen/router/UriRouter.java delete mode 100644 samples/src/main/java/fi/iki/elonen/router/UserHandler.java diff --git a/samples/src/main/java/fi/iki/elonen/router/App.java b/samples/src/main/java/fi/iki/elonen/router/App.java deleted file mode 100644 index e43e025..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/App.java +++ /dev/null @@ -1,86 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * 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% - */ - -/** - * Created by vnnv on 7/17/15. - * Simple httpd server based on NanoHTTPD - * Read the source. Everything is there. - */ - -import fi.iki.elonen.ServerRunner; - -import java.io.IOException; - -public class App extends RouterNanoHTTPD { - - private static final int PORT = 8081; - - /** - Create the server instance - */ - public App() throws IOException { - super(PORT); - addMappings(); - System.out.println("\nRunning! Point your browers to http://localhost:" + PORT + "/ \n"); - } - - /** - * Add the routes - * Every route is an absolute path - * Parameters starts with ":" - * Handler class should implement @UriResponder interface - * If the handler not implement UriResponder interface - toString() is used - */ - @Override - public void addMappings() { - super.addMappings(); - addRoute("/user", UserHandler.class); - addRoute("/user/:id", UserHandler.class); - addRoute("/user/help", GeneralHandler.class); - addRoute("/photos/:customer_id/:photo_id", null); - addRoute("/test", String.class); - } - - /** - * Main entry point - * @param args - */ - public static void main(String[] args) { - try { - ServerRunner.run(App.class); - } catch (Exception ioe) { - System.err.println("Couldn't start server:\n" + ioe); - } - } -} \ No newline at end of file diff --git a/samples/src/main/java/fi/iki/elonen/router/AppNanolets.java b/samples/src/main/java/fi/iki/elonen/router/AppNanolets.java new file mode 100644 index 0000000..ac35723 --- /dev/null +++ b/samples/src/main/java/fi/iki/elonen/router/AppNanolets.java @@ -0,0 +1,86 @@ +package fi.iki.elonen.router; +/* + * #%L + * NanoHttpd-Samples + * %% + * 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% + */ + +/** + * Created by vnnv on 7/17/15. + * Simple httpd server based on NanoHTTPD + * Read the source. Everything is there. + */ + +import fi.iki.elonen.ServerRunner; + +import java.io.IOException; + +public class AppNanolets extends RouterNanoHTTPD { + + private static final int PORT = 8081; + + /** + Create the server instance + */ + public AppNanolets() throws IOException { + super(PORT); + addMappings(); + System.out.println("\nRunning! Point your browers to http://localhost:" + PORT + "/ \n"); + } + + /** + * Add the routes + * Every route is an absolute path + * Parameters starts with ":" + * Handler class should implement @UriResponder interface + * If the handler not implement UriResponder interface - toString() is used + */ + @Override + public void addMappings() { + super.addMappings(); + addRoute("/user", UserHandler.class); + addRoute("/user/:id", UserHandler.class); + addRoute("/user/help", GeneralHandler.class); + addRoute("/photos/:customer_id/:photo_id", null); + addRoute("/test", String.class); + } + + /** + * Main entry point + * @param args + */ + public static void main(String[] args) { + try { + ServerRunner.run(AppNanolets.class); + } catch (Exception ioe) { + System.err.println("Couldn't start server:\n" + ioe); + } + } +} \ No newline at end of file diff --git a/samples/src/main/java/fi/iki/elonen/router/DefaultHandler.java b/samples/src/main/java/fi/iki/elonen/router/DefaultHandler.java deleted file mode 100644 index 57e7b47..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/DefaultHandler.java +++ /dev/null @@ -1,78 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * 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 fi.iki.elonen.NanoHTTPD; - -import java.io.ByteArrayInputStream; -import java.util.Map; - -/** - * Created by vnnv on 7/21/15. - * - */ -public abstract class DefaultHandler implements UriResponder { - - public abstract String getText(); - public abstract String getMimeType(); - public abstract NanoHTTPD.Response.IStatus getStatus(); - - public RouterResponse get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - String text = getText(); - ByteArrayInputStream inp = new ByteArrayInputStream(text.getBytes()); - - RouterResponse result = new RouterResponse(); - result.setData(inp); - result.setMimeType(getMimeType()); - result.setSize(text.getBytes().length); - result.setStatus(getStatus()); - - return result; - } - - public RouterResponse post(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - - public RouterResponse put(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - - public RouterResponse delete(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - public RouterResponse other(String method, UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - -} diff --git a/samples/src/main/java/fi/iki/elonen/router/GeneralHandler.java b/samples/src/main/java/fi/iki/elonen/router/GeneralHandler.java deleted file mode 100644 index 4e7f56a..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/GeneralHandler.java +++ /dev/null @@ -1,82 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * 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 fi.iki.elonen.NanoHTTPD; - -import java.io.ByteArrayInputStream; -import java.util.Map; - -/** - * Created by vnnv on 7/22/15. - */ -public class GeneralHandler extends DefaultHandler { - - @Override - public String getText() { - return null; - } - - @Override - public String getMimeType() { - return "text/html"; - } - - @Override - public NanoHTTPD.Response.IStatus getStatus() { - return NanoHTTPD.Response.Status.OK; - } - - public RouterResponse get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - String text = ""; - text += "

Url: " + session.getUri() + "


"; - Map queryParams = session.getParms(); - if (queryParams.size() > 0) { - for (Map.Entry entry : queryParams.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - text += "

Param '" + key + "' = " + value + "

"; - } - }else{ - text +="

no params in url


"; - } - - RouterResponse res = new RouterResponse(); - res.setData(new ByteArrayInputStream(text.getBytes())); - res.setSize(text.getBytes().length); - res.setStatus(getStatus()); - res.setMimeType(getMimeType()); - return res; - } - -} diff --git a/samples/src/main/java/fi/iki/elonen/router/RouterResponse.java b/samples/src/main/java/fi/iki/elonen/router/RouterResponse.java deleted file mode 100644 index ed16524..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/RouterResponse.java +++ /dev/null @@ -1,80 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * 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 fi.iki.elonen.NanoHTTPD; - -import java.io.InputStream; - -/** - * Created by vnnv on 7/21/15. - * Transport class because NanoHTTPD.Response has protected constructor - */ -public class RouterResponse { - private NanoHTTPD.Response.IStatus status; - private InputStream data; - private long size; - private String mimeType; - - public NanoHTTPD.Response.IStatus getStatus() { - return status; - } - - public void setStatus(NanoHTTPD.Response.IStatus status) { - this.status = status; - } - - public InputStream getData() { - return data; - } - - public void setData(InputStream data) { - this.data = data; - } - - public long getSize() { - return size; - } - - public void setSize(long size) { - this.size = size; - } - - public String getMimeType() { - return mimeType; - } - - public void setMimeType(String mimeType) { - this.mimeType = mimeType; - } -} diff --git a/samples/src/main/java/fi/iki/elonen/router/StringUtils.java b/samples/src/main/java/fi/iki/elonen/router/StringUtils.java deleted file mode 100644 index bacaceb..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/StringUtils.java +++ /dev/null @@ -1,54 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * 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% - */ - -/** - * Created by victor on 7/20/15. - * Util methods - */ -public class StringUtils { - public static String normalizeUri(String value) { - if (value == null) { - return value; - } - if (value.startsWith("/")) { - value = value.substring(1); - } - if (value.endsWith("/")) { - value = value.substring(0, value.length() - 1); - } - return value; - - } - -} diff --git a/samples/src/main/java/fi/iki/elonen/router/UriPart.java b/samples/src/main/java/fi/iki/elonen/router/UriPart.java deleted file mode 100644 index 19225b3..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/UriPart.java +++ /dev/null @@ -1,63 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * 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% - */ - -/** - * Created by vnnv on 7/20/15. - */ -public class UriPart { - private String name; - private boolean isParam; - - public UriPart(String name, boolean isParam) { - this.name = name; - this.isParam = isParam; - } - - @Override - public String toString() { - return "UriPart{" + - "name='" + name + '\'' + - ", isParam=" + isParam + - '}'; - } - - public boolean isParam() { - return isParam; - } - - public String getName() { - return name; - } - -} diff --git a/samples/src/main/java/fi/iki/elonen/router/UriResource.java b/samples/src/main/java/fi/iki/elonen/router/UriResource.java deleted file mode 100644 index 0ec45fe..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/UriResource.java +++ /dev/null @@ -1,149 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * 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 fi.iki.elonen.NanoHTTPD; - -import java.io.ByteArrayInputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -/** - * Created by vnnv on 7/20/15. - * Represent single uri - */ -public class UriResource { - private boolean hasParameters; - private int uriParamsCount; - private String uri; - private List uriParts; - private Class handler; - - public UriResource(String uri, Class handler) { - this.hasParameters = false; - this.handler = handler; - uriParamsCount = 0; - if (uri != null) { - this.uri = StringUtils.normalizeUri(uri); - parse(); - } - } - - private void parse(){ - String[] parts = uri.split("/"); - uriParts = new ArrayList(); - for (String part : parts) { - boolean isParam = part.startsWith(":"); - UriPart uriPart = null; - if (isParam) { - hasParameters = true; - uriParamsCount++; - uriPart = new UriPart(part.substring(1), true); - }else{ - uriPart = new UriPart(part, false); - } - uriParts.add(uriPart); - } - - } - - public RouterResponse process(Map urlParams, NanoHTTPD.IHTTPSession session) { - String error = "General error!"; - if (handler != null) { - try { - Object object = handler.newInstance(); - if (object instanceof UriResponder) { - UriResponder responder = (UriResponder) object; - switch (session.getMethod()) { - case GET: return responder.get(this, urlParams, session); - case POST: return responder.post(this, urlParams, session); - case PUT: return responder.put(this, urlParams, session); - case DELETE: return responder.delete(this, urlParams, session); - default: return responder.other(session.getMethod().toString(), this, urlParams, session); - } - }else { - // return toString() - String text = "Return: " + handler.getCanonicalName() + ".toString() -> " + object.toString(); - RouterResponse res = new RouterResponse(); - res.setStatus(NanoHTTPD.Response.Status.OK); - res.setMimeType("text/plain"); - res.setData(new ByteArrayInputStream(text.getBytes())); - res.setSize(text.getBytes().length); - return res; - } - } catch (InstantiationException e) { - error = "Error: " + InstantiationException.class.getName() + " : " + e.getMessage(); - e.printStackTrace(); - } catch (IllegalAccessException e) { - error = "Error: " + IllegalAccessException.class.getName() + " : " + e.getMessage(); - e.printStackTrace(); - } - } - - RouterResponse res = new RouterResponse(); - res.setStatus(NanoHTTPD.Response.Status.INTERNAL_ERROR); - res.setMimeType("text/plain"); - res.setData(new ByteArrayInputStream(error.getBytes())); - res.setSize(error.getBytes().length); - return res; - } - - - @Override - public String toString() { - return "UrlResource{" + - "hasParameters=" + hasParameters + - ", uriParamsCount=" + uriParamsCount + - ", uri='" + (uri != null ? "/" : "") + uri + '\'' + - ", urlParts=" + uriParts + - '}'; - } - - public boolean hasParameters() { - return hasParameters; - } - - public String getUri() { - return uri; - } - - public List getUriParts() { - return uriParts; - } - - public int getUriParamsCount() { - return uriParamsCount; - } - -} diff --git a/samples/src/main/java/fi/iki/elonen/router/UriResponder.java b/samples/src/main/java/fi/iki/elonen/router/UriResponder.java deleted file mode 100644 index 1015250..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/UriResponder.java +++ /dev/null @@ -1,50 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * 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 fi.iki.elonen.NanoHTTPD; - -import java.util.Map; - -/** - * Created by vnnv on 7/20/15. - */ -public interface UriResponder { - - public RouterResponse get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); - public RouterResponse put(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); - public RouterResponse post(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); - public RouterResponse delete(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); - public RouterResponse other(String method, UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); - -} diff --git a/samples/src/main/java/fi/iki/elonen/router/UriRouter.java b/samples/src/main/java/fi/iki/elonen/router/UriRouter.java deleted file mode 100644 index c28cecf..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/UriRouter.java +++ /dev/null @@ -1,182 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * 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.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Created by vnnv on 7/17/15. - */ -public class UriRouter { - - private List mappings; - private UriResource error404Url; - private Class notImplemented; - - public UriRouter() { - mappings = new ArrayList(); - } - - /** - * Search in the mappings if the given url matches some of the rules - * If there are more than one marches returns the rule with less parameters - * e.g. - * mapping 1 = /user/:id - * mapping 2 = /user/help - * - * if the incoming uri is www.example.com/user/help - mapping 2 is returned - * if the incoming uri is www.example.com/user/3232 - mapping 1 is returned - * - * @param url - * @return - */ - public UriResource matchUrl(String url) { - - String work = StringUtils.normalizeUri(url); - - String[] parts = work.split("/"); - List resultList = new ArrayList(); - - for (UriResource u : mappings) { - - // Check if count of parts is the same - if (parts.length != u.getUriParts().size()) { - continue; // different - } - - List uriParts = u.getUriParts(); - - boolean match = true; - for (int i = 0; i < parts.length; i++) { - String currentPart = parts[i]; - UriPart uriPart = uriParts.get(i); - boolean goOn = false; - - if (currentPart.equals(uriPart.getName())) { - // exact match - goOn = true; - }else { - // not match - if (uriPart.isParam()){ - goOn = true; - }else{ - match = false; - goOn = false; - } - } - if (!goOn) { - match = false; - break; - } - } // for - iterate incoming url parts - if (match) { - resultList.add(u); // current match - } - } // end iterate over all rules - if (resultList.size() > 0) { - // some results - UriResource result = null; - if (resultList.size() > 1) { - //return the rule with less parameters - int params = 1024; - for (UriResource u : resultList) { - if (!u.hasParameters()) { - result = u; - break; - }else{ - if (u.getUriParamsCount() <= params) { - result = u; - } - } - } - return result; - }else{ - return resultList.get(0); - } - } - return error404Url; - } - - public void addRoute(String url, Class handler) { - if (url != null) { - if (handler != null) { - mappings.add(new UriResource(url, handler)); - }else{ - mappings.add(new UriResource(url, notImplemented)); - } - } - - } - - public void removeRoute(String url) { - if (mappings.contains(url)) { - mappings.remove(url); - } - } - - public void setNotFoundHandler(Class handler) { - error404Url = new UriResource(null, handler); - } - - public void setNotImplemented(Class handler) { - notImplemented = handler; - } - - /** - * Extract parameters and their values for the given route - * @param route - * @param uri - * @return - */ - public Map getUrlParams(UriResource route, String uri) { - Map result = new HashMap(); - if (route.getUri() == null) { - return result; - } - - String workUri = StringUtils.normalizeUri(uri); - String[] parts = workUri.split("/"); - - - for (int i = 0; i < parts.length; i++ ) { - UriPart currentPart = route.getUriParts().get(i); - if (currentPart.isParam()) { - result.put(currentPart.getName(), parts[i]); - } - } - return result; - } -} diff --git a/samples/src/main/java/fi/iki/elonen/router/UserHandler.java b/samples/src/main/java/fi/iki/elonen/router/UserHandler.java deleted file mode 100644 index 8999793..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/UserHandler.java +++ /dev/null @@ -1,88 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * 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 fi.iki.elonen.NanoHTTPD; - -import java.io.ByteArrayInputStream; -import java.util.Map; - - - -/** - * Created by vnnv on 7/21/15. - */ -public class UserHandler implements UriResponder { - public RouterResponse get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - String text = "User handler. Method: " + session.getMethod().toString() + "
"; - text += "

Uri parameters:

"; - for (Map.Entry entry : urlParams.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - text += "
Param: " + key + " Value: " + value + "
"; - } - text += "

Query parameters:

"; - for (Map.Entry entry : session.getParms().entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - text += "
Query Param: " + key + " Value: " + value + "
"; - } - text += ""; - - ByteArrayInputStream inp = new ByteArrayInputStream(text.getBytes()); - - RouterResponse result = new RouterResponse(); - result.setData(inp); - result.setMimeType("text/html"); - result.setSize(text.getBytes().length); - result.setStatus(NanoHTTPD.Response.Status.OK); - - return result; - } - - public RouterResponse post(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - - public RouterResponse put(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - - public RouterResponse delete(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - - public RouterResponse other(String method, UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } -} -- cgit v1.2.3 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 +- samples/pom.xml | 2 +- .../java/fi/iki/elonen/router/AppNanolets.java | 136 +++-- .../java/fi/iki/elonen/router/RouterNanoHTTPD.java | 584 +++++++++++++++++---- 4 files changed, 573 insertions(+), 151 deletions(-) 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 diff --git a/samples/pom.xml b/samples/pom.xml index 620349c..5ff6764 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -3,7 +3,7 @@ org.nanohttpd nanohttpd-project - 2.2.0-SNAPSHOT + 2.2.1-SNAPSHOT nanohttpd-samples jar diff --git a/samples/src/main/java/fi/iki/elonen/router/AppNanolets.java b/samples/src/main/java/fi/iki/elonen/router/AppNanolets.java index ac35723..624487a 100644 --- a/samples/src/main/java/fi/iki/elonen/router/AppNanolets.java +++ b/samples/src/main/java/fi/iki/elonen/router/AppNanolets.java @@ -1,4 +1,5 @@ package fi.iki.elonen.router; + /* * #%L * NanoHttpd-Samples @@ -7,18 +8,18 @@ package fi.iki.elonen.router; * %% * 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. @@ -38,49 +39,96 @@ package fi.iki.elonen.router; * Read the source. Everything is there. */ +import fi.iki.elonen.NanoHTTPD; import fi.iki.elonen.ServerRunner; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.util.Map; public class AppNanolets extends RouterNanoHTTPD { - private static final int PORT = 8081; - - /** - Create the server instance - */ - public AppNanolets() throws IOException { - super(PORT); - addMappings(); - System.out.println("\nRunning! Point your browers to http://localhost:" + PORT + "/ \n"); - } - - /** - * Add the routes - * Every route is an absolute path - * Parameters starts with ":" - * Handler class should implement @UriResponder interface - * If the handler not implement UriResponder interface - toString() is used - */ - @Override - public void addMappings() { - super.addMappings(); - addRoute("/user", UserHandler.class); - addRoute("/user/:id", UserHandler.class); - addRoute("/user/help", GeneralHandler.class); - addRoute("/photos/:customer_id/:photo_id", null); - addRoute("/test", String.class); - } - - /** - * Main entry point - * @param args - */ - public static void main(String[] args) { - try { - ServerRunner.run(AppNanolets.class); - } catch (Exception ioe) { - System.err.println("Couldn't start server:\n" + ioe); - } - } -} \ No newline at end of file + private static final int PORT = 8080; + + public static class UserHandler extends DefaultHandler { + + @Override + public String getText() { + return "not implemented"; + } + + public String getText(Map urlParams, NanoHTTPD.IHTTPSession session) { + String text = "User handler. Method: " + session.getMethod().toString() + "
"; + text += "

Uri parameters:

"; + for (Map.Entry entry : urlParams.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + text += "
Param: " + key + " Value: " + value + "
"; + } + text += "

Query parameters:

"; + for (Map.Entry entry : session.getParms().entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + text += "
Query Param: " + key + " Value: " + value + "
"; + } + text += ""; + + return text; + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public NanoHTTPD.Response.IStatus getStatus() { + return NanoHTTPD.Response.Status.OK; + } + + public NanoHTTPD.Response get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + String text = getText(urlParams, session); + ByteArrayInputStream inp = new ByteArrayInputStream(text.getBytes()); + int size = text.getBytes().length; + return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), inp, size); + } + + } + + /** + * Create the server instance + */ + public AppNanolets() throws IOException { + super(PORT); + addMappings(); + System.out.println("\nRunning! Point your browers to http://localhost:" + PORT + "/ \n"); + } + + /** + * Add the routes Every route is an absolute path Parameters starts with ":" + * Handler class should implement @UriResponder interface If the handler not + * implement UriResponder interface - toString() is used + */ + @Override + public void addMappings() { + super.addMappings(); + addRoute("/user", UserHandler.class); + addRoute("/user/:id", UserHandler.class); + addRoute("/user/help", GeneralHandler.class); + addRoute("/photos/:customer_id/:photo_id", null); + addRoute("/test", String.class); + } + + /** + * Main entry point + * + * @param args + */ + public static void main(String[] args) { + try { + ServerRunner.run(AppNanolets.class); + } catch (Exception ioe) { + System.err.println("Couldn't start server:\n" + ioe); + } + } +} diff --git a/samples/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java b/samples/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java index 5da177c..8b1e8df 100644 --- a/samples/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java +++ b/samples/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java @@ -1,4 +1,5 @@ package fi.iki.elonen.router; + /* * #%L * NanoHttpd-Samples @@ -7,18 +8,18 @@ package fi.iki.elonen.router; * %% * 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. @@ -34,6 +35,11 @@ package fi.iki.elonen.router; import fi.iki.elonen.NanoHTTPD; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -41,105 +47,473 @@ import java.util.Map; */ public class RouterNanoHTTPD extends NanoHTTPD { - /** - * Handling error 404 - unrecognized urls - */ - public static class Error404UriHandler extends DefaultHandler { - - public String getText() { - String res = "

Error 404: " - + "the requested page doesn't exist.

"; - return res; - } - - @Override - public String getMimeType() { - return "text/html"; - } - - @Override - public Response.IStatus getStatus() { - return Response.Status.NOT_FOUND; - } - - } - - /** - * Handling index - */ - public static class IndexHandler extends DefaultHandler { - - public String getText() { - String res = "

Hello world!

"; - return res; - } - - @Override - public String getMimeType() { - return "text/html"; - } - - @Override - public Response.IStatus getStatus() { - return Response.Status.OK; - } - - } - - public static class NotImplementedHandler extends DefaultHandler { - - public String getText() { - String res = "

The uri is mapped in the router, " - + "but no handler is specified.
" - + "Status: Not implemented!

"; - return res; - } - - @Override - public String getMimeType() { - return "text/html"; - } - - @Override - public Response.IStatus getStatus() { - return Response.Status.OK; - } - - } - - - private UriRouter router; - - public RouterNanoHTTPD(int port) { - super(port); - router = new UriRouter(); - } - - public void addMappings() { - router.setNotImplemented(NotImplementedHandler.class); - router.setNotFoundHandler(Error404UriHandler.class); -// router.setNotFoundHandler(GeneralHandler.class); // You can use this instead of Error404UriHandler - router.addRoute("/", IndexHandler.class); - router.addRoute("/index.html", IndexHandler.class); - } - - public void addRoute(String url, Class handler) { - router.addRoute(url, handler); - } - - public void removeRoute(String url) { - router.removeRoute(url); - } - - @Override - public Response serve(IHTTPSession session) { - // Try to find match - UriResource uriResource = router.matchUrl(session.getUri()); - // Extract uri parameters - Map urlParams = router.getUrlParams(uriResource, session.getUri()); - // Process the uri - RouterResponse result = uriResource.process(urlParams, session); - // Return the response - return newFixedLengthResponse(result.getStatus(), result.getMimeType(), result.getData(), result.getSize()); - } + public interface UriResponder { + + public NanoHTTPD.Response get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); + + public NanoHTTPD.Response put(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); + + public NanoHTTPD.Response post(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); + + public NanoHTTPD.Response delete(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); + + public NanoHTTPD.Response other(String method, UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); + } // end interface UriResponder + + public static abstract class DefaultHandler implements UriResponder { + + public abstract String getText(); + + public abstract String getMimeType(); + + public abstract NanoHTTPD.Response.IStatus getStatus(); + + public NanoHTTPD.Response get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + String text = getText(); + ByteArrayInputStream inp = new ByteArrayInputStream(text.getBytes()); + int size = text.getBytes().length; + return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), inp, size); + } + + public NanoHTTPD.Response post(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + return get(uriResource, urlParams, session); + } + + public NanoHTTPD.Response put(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + return get(uriResource, urlParams, session); + } + + public NanoHTTPD.Response delete(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + return get(uriResource, urlParams, session); + } + + public NanoHTTPD.Response other(String method, UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + return get(uriResource, urlParams, session); + } + } // end Default Handler + + public static class GeneralHandler extends DefaultHandler { + + @Override + public String getText() { + return null; + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public NanoHTTPD.Response.IStatus getStatus() { + return NanoHTTPD.Response.Status.OK; + } + + public NanoHTTPD.Response get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + String text = ""; + text += "

Url: " + session.getUri() + "


"; + Map queryParams = session.getParms(); + if (queryParams.size() > 0) { + for (Map.Entry entry : queryParams.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + text += "

Param '" + key + "' = " + value + "

"; + } + } else { + text += "

no params in url


"; + } + + InputStream inp = new ByteArrayInputStream(text.getBytes()); + int length = text.getBytes().length; + + NanoHTTPD.Response res = NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), inp, length); + return res; + } + } // end General Handler + + /** + * Handling error 404 - unrecognized urls + */ + public static class Error404UriHandler extends DefaultHandler { + + public String getText() { + String res = "

Error 404: " + "the requested page doesn't exist.

"; + return res; + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public Response.IStatus getStatus() { + return Response.Status.NOT_FOUND; + } + } // End Error404UriHandler + + /** + * Handling index + */ + public static class IndexHandler extends DefaultHandler { + + public String getText() { + String res = "

Hello world!

"; + return res; + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public Response.IStatus getStatus() { + return Response.Status.OK; + } + + } // End IndexHanfler + + public static class NotImplementedHandler extends DefaultHandler { + + public String getText() { + String res = "

The uri is mapped in the router, " + "but no handler is specified.
" + "Status: Not implemented!

"; + return res; + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public Response.IStatus getStatus() { + return Response.Status.OK; + } + } // End NotImplementedHandler + + public static class UriUtils { + + public static String normalizeUri(String value) { + if (value == null) { + return value; + } + if (value.startsWith("/")) { + value = value.substring(1); + } + if (value.endsWith("/")) { + value = value.substring(0, value.length() - 1); + } + return value; + + } + + } + + public static class UriPart { + + private String name; + + private boolean isParam; + + public UriPart(String name, boolean isParam) { + this.name = name; + this.isParam = isParam; + } + + @Override + public String toString() { + return "UriPart{" + "name='" + name + '\'' + ", isParam=" + isParam + '}'; + } + + public boolean isParam() { + return isParam; + } + + public String getName() { + return name; + } + + } // End UriPart + + public static class UriResource { + + private boolean hasParameters; + + private int uriParamsCount; + + private String uri; + + private List uriParts; + + private Class handler; + + public UriResource(String uri, Class handler) { + this.hasParameters = false; + this.handler = handler; + uriParamsCount = 0; + if (uri != null) { + this.uri = UriUtils.normalizeUri(uri); + parse(); + } + } + + private void parse() { + String[] parts = uri.split("/"); + uriParts = new ArrayList(); + for (String part : parts) { + boolean isParam = part.startsWith(":"); + UriPart uriPart = null; + if (isParam) { + hasParameters = true; + uriParamsCount++; + uriPart = new UriPart(part.substring(1), true); + } else { + uriPart = new UriPart(part, false); + } + uriParts.add(uriPart); + } + + } + + public NanoHTTPD.Response process(Map urlParams, NanoHTTPD.IHTTPSession session) { + String error = "General error!"; + if (handler != null) { + try { + Object object = handler.newInstance(); + if (object instanceof UriResponder) { + UriResponder responder = (UriResponder) object; + switch (session.getMethod()) { + case GET: + return responder.get(this, urlParams, session); + case POST: + return responder.post(this, urlParams, session); + case PUT: + return responder.put(this, urlParams, session); + case DELETE: + return responder.delete(this, urlParams, session); + default: + return responder.other(session.getMethod().toString(), this, urlParams, session); + } + } else { + // return toString() + String text = "Return: " + handler.getCanonicalName() + ".toString() -> " + object.toString(); + NanoHTTPD.Response res = + NanoHTTPD + .newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "text/plain", new ByteArrayInputStream(text.getBytes()), text.getBytes().length); + return res; + } + } catch (InstantiationException e) { + error = "Error: " + InstantiationException.class.getName() + " : " + e.getMessage(); + e.printStackTrace(); + } catch (IllegalAccessException e) { + error = "Error: " + IllegalAccessException.class.getName() + " : " + e.getMessage(); + e.printStackTrace(); + } + } + + NanoHTTPD.Response res = + NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.INTERNAL_ERROR, "text/plain", new ByteArrayInputStream(error.getBytes()), + error.getBytes().length); + + return res; + } + + @Override + public String toString() { + return "UrlResource{" + "hasParameters=" + hasParameters + ", uriParamsCount=" + uriParamsCount + ", uri='" + (uri != null ? "/" : "") + uri + '\'' + + ", urlParts=" + uriParts + '}'; + } + + public boolean hasParameters() { + return hasParameters; + } + + public String getUri() { + return uri; + } + + public List getUriParts() { + return uriParts; + } + + public int getUriParamsCount() { + return uriParamsCount; + } + + } // End UriResource + + public static class UriRouter { + + private List mappings; + + private UriResource error404Url; + + private Class notImplemented; + + public UriRouter() { + mappings = new ArrayList(); + } + + /** + * Search in the mappings if the given url matches some of the rules If + * there are more than one marches returns the rule with less parameters + * e.g. mapping 1 = /user/:id mapping 2 = /user/help if the incoming uri + * is www.example.com/user/help - mapping 2 is returned if the incoming + * uri is www.example.com/user/3232 - mapping 1 is returned + * + * @param url + * @return + */ + public UriResource matchUrl(String url) { + + String work = UriUtils.normalizeUri(url); + + String[] parts = work.split("/"); + List resultList = new ArrayList(); + + for (UriResource u : mappings) { + + // Check if count of parts is the same + if (parts.length != u.getUriParts().size()) { + continue; // different + } + + List uriParts = u.getUriParts(); + + boolean match = true; + for (int i = 0; i < parts.length; i++) { + String currentPart = parts[i]; + UriPart uriPart = uriParts.get(i); + boolean goOn = false; + + if (currentPart.equals(uriPart.getName())) { + // exact match + goOn = true; + } else { + // not match + if (uriPart.isParam()) { + goOn = true; + } else { + match = false; + goOn = false; + } + } + if (!goOn) { + match = false; + break; + } + } // for - iterate incoming url parts + if (match) { + resultList.add(u); // current match + } + } // end iterate over all rules + if (resultList.size() > 0) { + // some results + UriResource result = null; + if (resultList.size() > 1) { + // return the rule with less parameters + int params = 1024; + for (UriResource u : resultList) { + if (!u.hasParameters()) { + result = u; + break; + } else { + if (u.getUriParamsCount() <= params) { + result = u; + } + } + } + return result; + } else { + return resultList.get(0); + } + } + return error404Url; + } + + public void addRoute(String url, Class handler) { + if (url != null) { + if (handler != null) { + mappings.add(new UriResource(url, handler)); + } else { + mappings.add(new UriResource(url, notImplemented)); + } + } + + } + + public void removeRoute(String url) { + if (mappings.contains(url)) { + mappings.remove(url); + } + } + + public void setNotFoundHandler(Class handler) { + error404Url = new UriResource(null, handler); + } + + public void setNotImplemented(Class handler) { + notImplemented = handler; + } + + /** + * Extract parameters and their values for the given route + * + * @param route + * @param uri + * @return + */ + public Map getUrlParams(UriResource route, String uri) { + Map result = new HashMap(); + if (route.getUri() == null) { + return result; + } + + String workUri = UriUtils.normalizeUri(uri); + String[] parts = workUri.split("/"); + + for (int i = 0; i < parts.length; i++) { + UriPart currentPart = route.getUriParts().get(i); + if (currentPart.isParam()) { + result.put(currentPart.getName(), parts[i]); + } + } + return result; + } + } // End UriRouter + + private UriRouter router; + + public RouterNanoHTTPD(int port) { + super(port); + router = new UriRouter(); + } + + public void addMappings() { + router.setNotImplemented(NotImplementedHandler.class); + router.setNotFoundHandler(Error404UriHandler.class); + // router.setNotFoundHandler(GeneralHandler.class); // You can use this + // instead of Error404UriHandler + router.addRoute("/", IndexHandler.class); + router.addRoute("/index.html", IndexHandler.class); + } + + public void addRoute(String url, Class handler) { + router.addRoute(url, handler); + } + + public void removeRoute(String url) { + router.removeRoute(url); + } + + @Override + public Response serve(IHTTPSession session) { + // Try to find match + UriResource uriResource = router.matchUrl(session.getUri()); + // Extract uri parameters + Map urlParams = router.getUrlParams(uriResource, session.getUri()); + // Process the uri + return uriResource.process(urlParams, session); + } } -- 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(-) 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 +++ .../.settings/org.eclipse.core.resources.prefs | 5 + nanolets/.settings/org.eclipse.jdt.core.prefs | 5 + nanolets/.settings/org.eclipse.m2e.core.prefs | 4 + nanolets/pom.xml | 19 + .../java/fi/iki/elonen/router/RouterNanoHTTPD.java | 545 +++++++++++++++++++++ .../java/fi/iki/elonen/router/AppNanolets.java | 130 +++++ pom.xml | 3 +- samples/pom.xml | 2 +- .../src/main/java/fi/iki/elonen/HelloServer.java | 2 + .../main/java/fi/iki/elonen/TempFilesServer.java | 1 + .../main/java/fi/iki/elonen/debug/DebugServer.java | 2 +- .../java/fi/iki/elonen/router/AppNanolets.java | 134 ----- .../java/fi/iki/elonen/router/RouterNanoHTTPD.java | 519 -------------------- webserver/pom.xml | 2 +- .../src/main/java/fi/iki/elonen/ServerRunner.java | 73 --- .../main/java/fi/iki/elonen/SimpleWebServer.java | 1 + 18 files changed, 793 insertions(+), 731 deletions(-) create mode 100644 core/src/main/java/fi/iki/elonen/util/ServerRunner.java create mode 100644 nanolets/.settings/org.eclipse.core.resources.prefs create mode 100644 nanolets/.settings/org.eclipse.jdt.core.prefs create mode 100644 nanolets/.settings/org.eclipse.m2e.core.prefs create mode 100644 nanolets/pom.xml create mode 100644 nanolets/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java create mode 100644 nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java delete mode 100644 samples/src/main/java/fi/iki/elonen/router/AppNanolets.java delete mode 100644 samples/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java delete mode 100644 webserver/src/main/java/fi/iki/elonen/ServerRunner.java 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); + } + } +} diff --git a/nanolets/.settings/org.eclipse.core.resources.prefs b/nanolets/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..839d647 --- /dev/null +++ b/nanolets/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/nanolets/.settings/org.eclipse.jdt.core.prefs b/nanolets/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..60105c1 --- /dev/null +++ b/nanolets/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/nanolets/.settings/org.eclipse.m2e.core.prefs b/nanolets/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/nanolets/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/nanolets/pom.xml b/nanolets/pom.xml new file mode 100644 index 0000000..6cf1c3f --- /dev/null +++ b/nanolets/pom.xml @@ -0,0 +1,19 @@ + + 4.0.0 + + org.nanohttpd + nanohttpd-project + 2.2.0-SNAPSHOT + + nanohttpd-nanolets + jar + NanoHttpd nano application server + + + ${project.groupId} + nanohttpd + ${project.version} + + + diff --git a/nanolets/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java b/nanolets/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java new file mode 100644 index 0000000..f347741 --- /dev/null +++ b/nanolets/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java @@ -0,0 +1,545 @@ +package fi.iki.elonen.router; + +/* + * #%L + * NanoHttpd-Samples + * %% + * 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.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +import fi.iki.elonen.NanoHTTPD; +import fi.iki.elonen.NanoHTTPD.Response.IStatus; +import fi.iki.elonen.NanoHTTPD.Response.Status; + +/** + * @author vnnv + * @author ritchieGitHub + */ +public class RouterNanoHTTPD extends NanoHTTPD { + + /** + * logger to log to. + */ + private static final Logger LOG = Logger.getLogger(RouterNanoHTTPD.class.getName()); + + public interface UriResponder { + + public Response get(UriResource uriResource, Map urlParams, IHTTPSession session); + + public Response put(UriResource uriResource, Map urlParams, IHTTPSession session); + + public Response post(UriResource uriResource, Map urlParams, IHTTPSession session); + + public Response delete(UriResource uriResource, Map urlParams, IHTTPSession session); + + public Response other(String method, UriResource uriResource, Map urlParams, IHTTPSession session); + } + + /** + * General nanolet to inherit from if you provide stream data, only chucked + * responses will be generated. + */ + public static abstract class DefaultStreamHandler implements UriResponder { + + public abstract String getMimeType(); + + public abstract IStatus getStatus(); + + public abstract InputStream getData(); + + public Response get(UriResource uriResource, Map urlParams, IHTTPSession session) { + return NanoHTTPD.newChunkedResponse(getStatus(), getMimeType(), getData()); + } + + public Response post(UriResource uriResource, Map urlParams, IHTTPSession session) { + return get(uriResource, urlParams, session); + } + + public Response put(UriResource uriResource, Map urlParams, IHTTPSession session) { + return get(uriResource, urlParams, session); + } + + public Response delete(UriResource uriResource, Map urlParams, IHTTPSession session) { + return get(uriResource, urlParams, session); + } + + public Response other(String method, UriResource uriResource, Map urlParams, IHTTPSession session) { + return get(uriResource, urlParams, session); + } + } + + /** + * General nanolet to inherit from if you provide text or html data, only + * fixed size responses will be generated. + */ + public static abstract class DefaultHandler extends DefaultStreamHandler { + + public abstract String getText(); + + public abstract IStatus getStatus(); + + public Response get(UriResource uriResource, Map urlParams, IHTTPSession session) { + return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), getText()); + } + + @Override + public InputStream getData() { + throw new IllegalStateException("this method should not be called in a text based nanolet"); + } + } + + /** + * General nanolet to print debug info's as a html page. + */ + public static class GeneralHandler extends DefaultHandler { + + @Override + public String getText() { + throw new IllegalStateException("this method should not be called"); + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public IStatus getStatus() { + return Status.OK; + } + + public Response get(UriResource uriResource, Map urlParams, IHTTPSession session) { + StringBuilder text = new StringBuilder(""); + text.append("

Url: "); + text.append(session.getUri()); + text.append("


"); + Map queryParams = session.getParms(); + if (queryParams.size() > 0) { + for (Map.Entry entry : queryParams.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + text.append("

Param '"); + text.append(key); + text.append("' = "); + text.append(value); + text.append("

"); + } + } else { + text.append("

no params in url


"); + } + return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), text.toString()); + } + } + + /** + * Handling error 404 - unrecognized urls + */ + public static class Error404UriHandler extends DefaultHandler { + + public String getText() { + return "

Error 404: the requested page doesn't exist.

"; + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public IStatus getStatus() { + return Status.NOT_FOUND; + } + } + + /** + * Handling index + */ + public static class IndexHandler extends DefaultHandler { + + public String getText() { + return "

Hello world!

"; + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public IStatus getStatus() { + return Status.OK; + } + + } + + public static class NotImplementedHandler extends DefaultHandler { + + public String getText() { + return "

The uri is mapped in the router, but no handler is specified.
Status: Not implemented!

"; + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public IStatus getStatus() { + return Status.OK; + } + } + + public static class UriUtils { + + public static String normalizeUri(String value) { + if (value == null) { + return value; + } + if (value.startsWith("/")) { + value = value.substring(1); + } + if (value.endsWith("/")) { + value = value.substring(0, value.length() - 1); + } + return value; + + } + + } + + public static class UriPart { + + private String name; + + private boolean isParam; + + public UriPart(String name, boolean isParam) { + this.name = name; + this.isParam = isParam; + } + + @Override + public String toString() { + return "UriPart{name='" + name + '\'' + ", isParam=" + isParam + '}'; + } + + public boolean isParam() { + return isParam; + } + + public String getName() { + return name; + } + + } + + public static class UriResource { + + private boolean hasParameters; + + private int uriParamsCount; + + private String uri; + + private List uriParts; + + private Class handler; + + public UriResource(String uri, Class handler) { + this.hasParameters = false; + this.handler = handler; + uriParamsCount = 0; + if (uri != null) { + this.uri = UriUtils.normalizeUri(uri); + parse(); + } + } + + private void parse() { + String[] parts = uri.split("/"); + uriParts = new ArrayList(); + for (String part : parts) { + boolean isParam = part.startsWith(":"); + UriPart uriPart = null; + if (isParam) { + hasParameters = true; + uriParamsCount++; + uriPart = new UriPart(part.substring(1), true); + } else { + uriPart = new UriPart(part, false); + } + uriParts.add(uriPart); + } + + } + + public Response process(Map urlParams, IHTTPSession session) { + String error = "General error!"; + if (handler != null) { + try { + Object object = handler.newInstance(); + if (object instanceof UriResponder) { + UriResponder responder = (UriResponder) object; + switch (session.getMethod()) { + case GET: + return responder.get(this, urlParams, session); + case POST: + return responder.post(this, urlParams, session); + case PUT: + return responder.put(this, urlParams, session); + case DELETE: + return responder.delete(this, urlParams, session); + default: + return responder.other(session.getMethod().toString(), this, urlParams, session); + } + } else { + return NanoHTTPD.newFixedLengthResponse(Status.OK, "text/plain", // + new StringBuilder("Return: ")// + .append(handler.getCanonicalName())// + .append(".toString() -> ")// + .append(object)// + .toString()); + } + } catch (InstantiationException e) { + error = "Error: " + e.getClass().getName() + " : " + e.getMessage(); + LOG.log(Level.SEVERE, error, e); + } catch (IllegalAccessException e) { + error = "Error: " + e.getClass().getName() + " : " + e.getMessage(); + LOG.log(Level.SEVERE, error, e); + } + } + + return NanoHTTPD.newFixedLengthResponse(Status.INTERNAL_ERROR, "text/plain", error); + } + + @Override + public String toString() { + return new StringBuilder("UrlResource{hasParameters=").append(hasParameters)// + .append(", uriParamsCount=").append(uriParamsCount)// + .append(", uri='").append((uri == null ? "/" : uri))// + .append("', urlParts=").append(uriParts)// + .append('}')// + .toString(); + } + + public boolean hasParameters() { + return hasParameters; + } + + public String getUri() { + return uri; + } + + public List getUriParts() { + return uriParts; + } + + public int getUriParamsCount() { + return uriParamsCount; + } + + } + + public static class UriRouter { + + private List mappings; + + private UriResource error404Url; + + private Class notImplemented; + + public UriRouter() { + mappings = new ArrayList(); + } + + /** + * Search in the mappings if the given url matches some of the rules If + * there are more than one marches returns the rule with less parameters + * e.g. mapping 1 = /user/:id mapping 2 = /user/help if the incoming uri + * is www.example.com/user/help - mapping 2 is returned if the incoming + * uri is www.example.com/user/3232 - mapping 1 is returned + * + * @param url + * @return + */ + public UriResource matchUrl(String url) { + String work = UriUtils.normalizeUri(url); + String[] parts = work.split("/"); + List resultList = new ArrayList(); + for (UriResource u : mappings) { + // Check if count of parts is the same + if (parts.length != u.getUriParts().size()) { + continue; // different + } + List uriParts = u.getUriParts(); + boolean match = true; + for (int i = 0; i < parts.length; i++) { + String currentPart = parts[i]; + UriPart uriPart = uriParts.get(i); + boolean goOn = false; + if (currentPart.equals(uriPart.getName())) { + // exact match + goOn = true; + } else { + // not match + if (uriPart.isParam()) { + goOn = true; + } else { + match = false; + goOn = false; + } + } + if (!goOn) { + match = false; + break; + } + } + if (match) { + // current match + resultList.add(u); + } + } + if (!resultList.isEmpty()) { + // some results + UriResource result = null; + if (resultList.size() > 1) { + // return the rule with less parameters + int params = 1024; + for (UriResource u : resultList) { + if (!u.hasParameters()) { + result = u; + break; + } else { + if (u.getUriParamsCount() <= params) { + result = u; + } + } + } + return result; + } else { + return resultList.get(0); + } + } + return error404Url; + } + + public void addRoute(String url, Class handler) { + if (url != null) { + if (handler != null) { + mappings.add(new UriResource(url, handler)); + } else { + mappings.add(new UriResource(url, notImplemented)); + } + } + } + + public void removeRoute(String url) { + if (mappings.contains(url)) { + mappings.remove(url); + } + } + + public void setNotFoundHandler(Class handler) { + error404Url = new UriResource(null, handler); + } + + public void setNotImplemented(Class handler) { + notImplemented = handler; + } + + /** + * Extract parameters and their values for the given route + * + * @param route + * @param uri + * @return + */ + public Map getUrlParams(UriResource route, String uri) { + Map result = new HashMap(); + if (route.getUri() == null) { + return result; + } + String workUri = UriUtils.normalizeUri(uri); + String[] parts = workUri.split("/"); + for (int i = 0; i < parts.length; i++) { + UriPart currentPart = route.getUriParts().get(i); + if (currentPart.isParam()) { + result.put(currentPart.getName(), parts[i]); + } + } + return result; + } + } + + private UriRouter router; + + public RouterNanoHTTPD(int port) { + super(port); + router = new UriRouter(); + } + + /** + * default routings, they are over writable. + * + *
+     * router.setNotFoundHandler(GeneralHandler.class);
+     * 
+ */ + + public void addMappings() { + router.setNotImplemented(NotImplementedHandler.class); + router.setNotFoundHandler(Error404UriHandler.class); + router.addRoute("/", IndexHandler.class); + router.addRoute("/index.html", IndexHandler.class); + } + + public void addRoute(String url, Class handler) { + router.addRoute(url, handler); + } + + public void removeRoute(String url) { + router.removeRoute(url); + } + + @Override + public Response serve(IHTTPSession session) { + // Try to find match + UriResource uriResource = router.matchUrl(session.getUri()); + // Process the uri + return uriResource.process(router.getUrlParams(uriResource, session.getUri()), session); + } +} diff --git a/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java b/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java new file mode 100644 index 0000000..ae1e5af --- /dev/null +++ b/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java @@ -0,0 +1,130 @@ +package fi.iki.elonen.router; + +/* + * #%L + * NanoHttpd-Samples + * %% + * 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% + */ + +/** + * Created by vnnv on 7/17/15. + * Simple httpd server based on NanoHTTPD + * Read the source. Everything is there. + */ + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.Map; + +import fi.iki.elonen.NanoHTTPD; +import fi.iki.elonen.util.ServerRunner; + +public class AppNanolets extends RouterNanoHTTPD { + + private static final int PORT = 8080; + + public static class UserHandler extends DefaultHandler { + + @Override + public String getText() { + return "not implemented"; + } + + public String getText(Map urlParams, NanoHTTPD.IHTTPSession session) { + String text = "User handler. Method: " + session.getMethod().toString() + "
"; + text += "

Uri parameters:

"; + for (Map.Entry entry : urlParams.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + text += "
Param: " + key + " Value: " + value + "
"; + } + text += "

Query parameters:

"; + for (Map.Entry entry : session.getParms().entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + text += "
Query Param: " + key + " Value: " + value + "
"; + } + text += ""; + + return text; + } + + @Override + public String getMimeType() { + return "text/html"; + } + + @Override + public NanoHTTPD.Response.IStatus getStatus() { + return NanoHTTPD.Response.Status.OK; + } + + public NanoHTTPD.Response get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { + String text = getText(urlParams, session); + ByteArrayInputStream inp = new ByteArrayInputStream(text.getBytes()); + int size = text.getBytes().length; + return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), inp, size); + } + + } + + /** + * Create the server instance + */ + public AppNanolets() throws IOException { + super(PORT); + addMappings(); + System.out.println("\nRunning! Point your browers to http://localhost:" + PORT + "/ \n"); + } + + /** + * Add the routes Every route is an absolute path Parameters starts with ":" + * Handler class should implement @UriResponder interface If the handler not + * implement UriResponder interface - toString() is used + */ + @Override + public void addMappings() { + super.addMappings(); + addRoute("/user", UserHandler.class); + addRoute("/user/:id", UserHandler.class); + addRoute("/user/help", GeneralHandler.class); + addRoute("/photos/:customer_id/:photo_id", null); + addRoute("/test", String.class); + } + + /** + * Main entry point + * + * @param args + */ + public static void main(String[] args) { + ServerRunner.run(AppNanolets.class); + } +} diff --git a/pom.xml b/pom.xml index e2ea817..d6844ef 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ org.nanohttpd nanohttpd-project - 2.2.1-SNAPSHOT + 2.2.0-SNAPSHOT pom NanoHttpd-Project NanoHttpd is a light-weight HTTP server designed for embedding in other applications. @@ -88,6 +88,7 @@ webserver websocket markdown-plugin + nanolets diff --git a/samples/pom.xml b/samples/pom.xml index 5ff6764..620349c 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -3,7 +3,7 @@ org.nanohttpd nanohttpd-project - 2.2.1-SNAPSHOT + 2.2.0-SNAPSHOT nanohttpd-samples jar diff --git a/samples/src/main/java/fi/iki/elonen/HelloServer.java b/samples/src/main/java/fi/iki/elonen/HelloServer.java index 7226108..bc91231 100644 --- a/samples/src/main/java/fi/iki/elonen/HelloServer.java +++ b/samples/src/main/java/fi/iki/elonen/HelloServer.java @@ -36,6 +36,8 @@ package fi.iki.elonen; import java.util.Map; import java.util.logging.Logger; +import fi.iki.elonen.util.ServerRunner; + /** * An example of subclassing NanoHTTPD to make a custom HTTP server. */ diff --git a/samples/src/main/java/fi/iki/elonen/TempFilesServer.java b/samples/src/main/java/fi/iki/elonen/TempFilesServer.java index dbe7d37..8429e32 100644 --- a/samples/src/main/java/fi/iki/elonen/TempFilesServer.java +++ b/samples/src/main/java/fi/iki/elonen/TempFilesServer.java @@ -37,6 +37,7 @@ import java.util.ArrayList; import java.util.List; import fi.iki.elonen.debug.DebugServer; +import fi.iki.elonen.util.ServerRunner; /** * @author Paul S. Hawke (paul.hawke@gmail.com) On: 3/9/13 at 12:47 AM diff --git a/samples/src/main/java/fi/iki/elonen/debug/DebugServer.java b/samples/src/main/java/fi/iki/elonen/debug/DebugServer.java index 056673d..0ffc34f 100644 --- a/samples/src/main/java/fi/iki/elonen/debug/DebugServer.java +++ b/samples/src/main/java/fi/iki/elonen/debug/DebugServer.java @@ -38,7 +38,7 @@ import java.util.List; import java.util.Map; import fi.iki.elonen.NanoHTTPD; -import fi.iki.elonen.ServerRunner; +import fi.iki.elonen.util.ServerRunner; public class DebugServer extends NanoHTTPD { diff --git a/samples/src/main/java/fi/iki/elonen/router/AppNanolets.java b/samples/src/main/java/fi/iki/elonen/router/AppNanolets.java deleted file mode 100644 index 624487a..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/AppNanolets.java +++ /dev/null @@ -1,134 +0,0 @@ -package fi.iki.elonen.router; - -/* - * #%L - * NanoHttpd-Samples - * %% - * 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% - */ - -/** - * Created by vnnv on 7/17/15. - * Simple httpd server based on NanoHTTPD - * Read the source. Everything is there. - */ - -import fi.iki.elonen.NanoHTTPD; -import fi.iki.elonen.ServerRunner; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.util.Map; - -public class AppNanolets extends RouterNanoHTTPD { - - private static final int PORT = 8080; - - public static class UserHandler extends DefaultHandler { - - @Override - public String getText() { - return "not implemented"; - } - - public String getText(Map urlParams, NanoHTTPD.IHTTPSession session) { - String text = "User handler. Method: " + session.getMethod().toString() + "
"; - text += "

Uri parameters:

"; - for (Map.Entry entry : urlParams.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - text += "
Param: " + key + " Value: " + value + "
"; - } - text += "

Query parameters:

"; - for (Map.Entry entry : session.getParms().entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - text += "
Query Param: " + key + " Value: " + value + "
"; - } - text += ""; - - return text; - } - - @Override - public String getMimeType() { - return "text/html"; - } - - @Override - public NanoHTTPD.Response.IStatus getStatus() { - return NanoHTTPD.Response.Status.OK; - } - - public NanoHTTPD.Response get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - String text = getText(urlParams, session); - ByteArrayInputStream inp = new ByteArrayInputStream(text.getBytes()); - int size = text.getBytes().length; - return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), inp, size); - } - - } - - /** - * Create the server instance - */ - public AppNanolets() throws IOException { - super(PORT); - addMappings(); - System.out.println("\nRunning! Point your browers to http://localhost:" + PORT + "/ \n"); - } - - /** - * Add the routes Every route is an absolute path Parameters starts with ":" - * Handler class should implement @UriResponder interface If the handler not - * implement UriResponder interface - toString() is used - */ - @Override - public void addMappings() { - super.addMappings(); - addRoute("/user", UserHandler.class); - addRoute("/user/:id", UserHandler.class); - addRoute("/user/help", GeneralHandler.class); - addRoute("/photos/:customer_id/:photo_id", null); - addRoute("/test", String.class); - } - - /** - * Main entry point - * - * @param args - */ - public static void main(String[] args) { - try { - ServerRunner.run(AppNanolets.class); - } catch (Exception ioe) { - System.err.println("Couldn't start server:\n" + ioe); - } - } -} diff --git a/samples/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java b/samples/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java deleted file mode 100644 index 8b1e8df..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java +++ /dev/null @@ -1,519 +0,0 @@ -package fi.iki.elonen.router; - -/* - * #%L - * NanoHttpd-Samples - * %% - * 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 fi.iki.elonen.NanoHTTPD; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Created by vnnv on 7/21/15. - */ -public class RouterNanoHTTPD extends NanoHTTPD { - - public interface UriResponder { - - public NanoHTTPD.Response get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); - - public NanoHTTPD.Response put(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); - - public NanoHTTPD.Response post(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); - - public NanoHTTPD.Response delete(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); - - public NanoHTTPD.Response other(String method, UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session); - } // end interface UriResponder - - public static abstract class DefaultHandler implements UriResponder { - - public abstract String getText(); - - public abstract String getMimeType(); - - public abstract NanoHTTPD.Response.IStatus getStatus(); - - public NanoHTTPD.Response get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - String text = getText(); - ByteArrayInputStream inp = new ByteArrayInputStream(text.getBytes()); - int size = text.getBytes().length; - return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), inp, size); - } - - public NanoHTTPD.Response post(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - - public NanoHTTPD.Response put(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - - public NanoHTTPD.Response delete(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - - public NanoHTTPD.Response other(String method, UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - } // end Default Handler - - public static class GeneralHandler extends DefaultHandler { - - @Override - public String getText() { - return null; - } - - @Override - public String getMimeType() { - return "text/html"; - } - - @Override - public NanoHTTPD.Response.IStatus getStatus() { - return NanoHTTPD.Response.Status.OK; - } - - public NanoHTTPD.Response get(UriResource uriResource, Map urlParams, NanoHTTPD.IHTTPSession session) { - String text = ""; - text += "

Url: " + session.getUri() + "


"; - Map queryParams = session.getParms(); - if (queryParams.size() > 0) { - for (Map.Entry entry : queryParams.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - text += "

Param '" + key + "' = " + value + "

"; - } - } else { - text += "

no params in url


"; - } - - InputStream inp = new ByteArrayInputStream(text.getBytes()); - int length = text.getBytes().length; - - NanoHTTPD.Response res = NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), inp, length); - return res; - } - } // end General Handler - - /** - * Handling error 404 - unrecognized urls - */ - public static class Error404UriHandler extends DefaultHandler { - - public String getText() { - String res = "

Error 404: " + "the requested page doesn't exist.

"; - return res; - } - - @Override - public String getMimeType() { - return "text/html"; - } - - @Override - public Response.IStatus getStatus() { - return Response.Status.NOT_FOUND; - } - } // End Error404UriHandler - - /** - * Handling index - */ - public static class IndexHandler extends DefaultHandler { - - public String getText() { - String res = "

Hello world!

"; - return res; - } - - @Override - public String getMimeType() { - return "text/html"; - } - - @Override - public Response.IStatus getStatus() { - return Response.Status.OK; - } - - } // End IndexHanfler - - public static class NotImplementedHandler extends DefaultHandler { - - public String getText() { - String res = "

The uri is mapped in the router, " + "but no handler is specified.
" + "Status: Not implemented!

"; - return res; - } - - @Override - public String getMimeType() { - return "text/html"; - } - - @Override - public Response.IStatus getStatus() { - return Response.Status.OK; - } - } // End NotImplementedHandler - - public static class UriUtils { - - public static String normalizeUri(String value) { - if (value == null) { - return value; - } - if (value.startsWith("/")) { - value = value.substring(1); - } - if (value.endsWith("/")) { - value = value.substring(0, value.length() - 1); - } - return value; - - } - - } - - public static class UriPart { - - private String name; - - private boolean isParam; - - public UriPart(String name, boolean isParam) { - this.name = name; - this.isParam = isParam; - } - - @Override - public String toString() { - return "UriPart{" + "name='" + name + '\'' + ", isParam=" + isParam + '}'; - } - - public boolean isParam() { - return isParam; - } - - public String getName() { - return name; - } - - } // End UriPart - - public static class UriResource { - - private boolean hasParameters; - - private int uriParamsCount; - - private String uri; - - private List uriParts; - - private Class handler; - - public UriResource(String uri, Class handler) { - this.hasParameters = false; - this.handler = handler; - uriParamsCount = 0; - if (uri != null) { - this.uri = UriUtils.normalizeUri(uri); - parse(); - } - } - - private void parse() { - String[] parts = uri.split("/"); - uriParts = new ArrayList(); - for (String part : parts) { - boolean isParam = part.startsWith(":"); - UriPart uriPart = null; - if (isParam) { - hasParameters = true; - uriParamsCount++; - uriPart = new UriPart(part.substring(1), true); - } else { - uriPart = new UriPart(part, false); - } - uriParts.add(uriPart); - } - - } - - public NanoHTTPD.Response process(Map urlParams, NanoHTTPD.IHTTPSession session) { - String error = "General error!"; - if (handler != null) { - try { - Object object = handler.newInstance(); - if (object instanceof UriResponder) { - UriResponder responder = (UriResponder) object; - switch (session.getMethod()) { - case GET: - return responder.get(this, urlParams, session); - case POST: - return responder.post(this, urlParams, session); - case PUT: - return responder.put(this, urlParams, session); - case DELETE: - return responder.delete(this, urlParams, session); - default: - return responder.other(session.getMethod().toString(), this, urlParams, session); - } - } else { - // return toString() - String text = "Return: " + handler.getCanonicalName() + ".toString() -> " + object.toString(); - NanoHTTPD.Response res = - NanoHTTPD - .newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "text/plain", new ByteArrayInputStream(text.getBytes()), text.getBytes().length); - return res; - } - } catch (InstantiationException e) { - error = "Error: " + InstantiationException.class.getName() + " : " + e.getMessage(); - e.printStackTrace(); - } catch (IllegalAccessException e) { - error = "Error: " + IllegalAccessException.class.getName() + " : " + e.getMessage(); - e.printStackTrace(); - } - } - - NanoHTTPD.Response res = - NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.INTERNAL_ERROR, "text/plain", new ByteArrayInputStream(error.getBytes()), - error.getBytes().length); - - return res; - } - - @Override - public String toString() { - return "UrlResource{" + "hasParameters=" + hasParameters + ", uriParamsCount=" + uriParamsCount + ", uri='" + (uri != null ? "/" : "") + uri + '\'' - + ", urlParts=" + uriParts + '}'; - } - - public boolean hasParameters() { - return hasParameters; - } - - public String getUri() { - return uri; - } - - public List getUriParts() { - return uriParts; - } - - public int getUriParamsCount() { - return uriParamsCount; - } - - } // End UriResource - - public static class UriRouter { - - private List mappings; - - private UriResource error404Url; - - private Class notImplemented; - - public UriRouter() { - mappings = new ArrayList(); - } - - /** - * Search in the mappings if the given url matches some of the rules If - * there are more than one marches returns the rule with less parameters - * e.g. mapping 1 = /user/:id mapping 2 = /user/help if the incoming uri - * is www.example.com/user/help - mapping 2 is returned if the incoming - * uri is www.example.com/user/3232 - mapping 1 is returned - * - * @param url - * @return - */ - public UriResource matchUrl(String url) { - - String work = UriUtils.normalizeUri(url); - - String[] parts = work.split("/"); - List resultList = new ArrayList(); - - for (UriResource u : mappings) { - - // Check if count of parts is the same - if (parts.length != u.getUriParts().size()) { - continue; // different - } - - List uriParts = u.getUriParts(); - - boolean match = true; - for (int i = 0; i < parts.length; i++) { - String currentPart = parts[i]; - UriPart uriPart = uriParts.get(i); - boolean goOn = false; - - if (currentPart.equals(uriPart.getName())) { - // exact match - goOn = true; - } else { - // not match - if (uriPart.isParam()) { - goOn = true; - } else { - match = false; - goOn = false; - } - } - if (!goOn) { - match = false; - break; - } - } // for - iterate incoming url parts - if (match) { - resultList.add(u); // current match - } - } // end iterate over all rules - if (resultList.size() > 0) { - // some results - UriResource result = null; - if (resultList.size() > 1) { - // return the rule with less parameters - int params = 1024; - for (UriResource u : resultList) { - if (!u.hasParameters()) { - result = u; - break; - } else { - if (u.getUriParamsCount() <= params) { - result = u; - } - } - } - return result; - } else { - return resultList.get(0); - } - } - return error404Url; - } - - public void addRoute(String url, Class handler) { - if (url != null) { - if (handler != null) { - mappings.add(new UriResource(url, handler)); - } else { - mappings.add(new UriResource(url, notImplemented)); - } - } - - } - - public void removeRoute(String url) { - if (mappings.contains(url)) { - mappings.remove(url); - } - } - - public void setNotFoundHandler(Class handler) { - error404Url = new UriResource(null, handler); - } - - public void setNotImplemented(Class handler) { - notImplemented = handler; - } - - /** - * Extract parameters and their values for the given route - * - * @param route - * @param uri - * @return - */ - public Map getUrlParams(UriResource route, String uri) { - Map result = new HashMap(); - if (route.getUri() == null) { - return result; - } - - String workUri = UriUtils.normalizeUri(uri); - String[] parts = workUri.split("/"); - - for (int i = 0; i < parts.length; i++) { - UriPart currentPart = route.getUriParts().get(i); - if (currentPart.isParam()) { - result.put(currentPart.getName(), parts[i]); - } - } - return result; - } - } // End UriRouter - - private UriRouter router; - - public RouterNanoHTTPD(int port) { - super(port); - router = new UriRouter(); - } - - public void addMappings() { - router.setNotImplemented(NotImplementedHandler.class); - router.setNotFoundHandler(Error404UriHandler.class); - // router.setNotFoundHandler(GeneralHandler.class); // You can use this - // instead of Error404UriHandler - router.addRoute("/", IndexHandler.class); - router.addRoute("/index.html", IndexHandler.class); - } - - public void addRoute(String url, Class handler) { - router.addRoute(url, handler); - } - - public void removeRoute(String url) { - router.removeRoute(url); - } - - @Override - public Response serve(IHTTPSession session) { - // Try to find match - UriResource uriResource = router.matchUrl(session.getUri()); - // Extract uri parameters - Map urlParams = router.getUrlParams(uriResource, session.getUri()); - // Process the uri - return uriResource.process(urlParams, session); - } -} diff --git a/webserver/pom.xml b/webserver/pom.xml index 32b1f54..45f726b 100644 --- a/webserver/pom.xml +++ b/webserver/pom.xml @@ -4,7 +4,7 @@ org.nanohttpd nanohttpd-project - 2.2.1-SNAPSHOT + 2.2.0-SNAPSHOT nanohttpd-webserver jar diff --git a/webserver/src/main/java/fi/iki/elonen/ServerRunner.java b/webserver/src/main/java/fi/iki/elonen/ServerRunner.java deleted file mode 100644 index d08eb01..0000000 --- a/webserver/src/main/java/fi/iki/elonen/ServerRunner.java +++ /dev/null @@ -1,73 +0,0 @@ -package fi.iki.elonen; - -/* - * #%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; - -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); - } - } -} diff --git a/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java b/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java index 0ddc21c..f3ad1c2 100644 --- a/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java +++ b/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java @@ -51,6 +51,7 @@ import java.util.ServiceLoader; import java.util.StringTokenizer; import fi.iki.elonen.NanoHTTPD.Response.IStatus; +import fi.iki.elonen.util.ServerRunner; public class SimpleWebServer extends NanoHTTPD { -- cgit v1.2.3 From d66d5b8916ccfb23f894ff1a3936e3f7462701f1 Mon Sep 17 00:00:00 2001 From: ritchie Date: Fri, 11 Sep 2015 10:07:31 +0200 Subject: first test case for nanolets #214 --- nanolets/LICENSE.txt | 26 +++++ nanolets/pom.xml | 6 ++ .../java/fi/iki/elonen/router/AppNanolets.java | 2 +- .../java/fi/iki/elonen/router/TestNanolets.java | 116 +++++++++++++++++++++ 4 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 nanolets/LICENSE.txt create mode 100644 nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java diff --git a/nanolets/LICENSE.txt b/nanolets/LICENSE.txt new file mode 100644 index 0000000..fcebfe1 --- /dev/null +++ b/nanolets/LICENSE.txt @@ -0,0 +1,26 @@ +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. diff --git a/nanolets/pom.xml b/nanolets/pom.xml index 6cf1c3f..39af427 100644 --- a/nanolets/pom.xml +++ b/nanolets/pom.xml @@ -15,5 +15,11 @@ nanohttpd ${project.version} + + org.apache.httpcomponents + httpclient + 4.4.1 + test +
diff --git a/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java b/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java index ae1e5af..f69a611 100644 --- a/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java +++ b/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java @@ -48,7 +48,7 @@ import fi.iki.elonen.util.ServerRunner; public class AppNanolets extends RouterNanoHTTPD { - private static final int PORT = 8080; + private static final int PORT = 9090; public static class UserHandler extends DefaultHandler { diff --git a/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java b/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java new file mode 100644 index 0000000..0d35868 --- /dev/null +++ b/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java @@ -0,0 +1,116 @@ +package fi.iki.elonen.router; + +/* + * #%L + * NanoHttpd nano application server + * %% + * 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.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; + +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestNanolets { + + private static PipedOutputStream stdIn; + + private static Thread serverStartThread; + + @BeforeClass + public static void setUp() throws Exception { + stdIn = new PipedOutputStream(); + System.setIn(new PipedInputStream(stdIn)); + serverStartThread = new Thread(new Runnable() { + + @Override + public void run() { + String[] args = {}; + AppNanolets.main(args); + } + }); + serverStartThread.start(); + // give the server some tine to start. + Thread.sleep(100); + } + + @Test + public void doSomeBasicTest() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpget = new HttpGet("http://localhost:9090/user/blabla"); + CloseableHttpResponse response = httpclient.execute(httpget); + HttpEntity entity = response.getEntity(); + String string = new String(readContents(entity), "UTF-8"); + Assert.assertEquals( + "User handler. Method: GET

Uri parameters:

Param: id Value: blabla

Query parameters:

", string); + response.close(); + } + + private byte[] readContents(HttpEntity entity) throws IOException { + InputStream instream = entity.getContent(); + return readContents(instream); + } + + private byte[] readContents(InputStream instream) throws IOException { + byte[] bytes; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + try { + byte[] buffer = new byte[1024]; + int count; + while ((count = instream.read(buffer)) >= 0) { + out.write(buffer, 0, count); + } + bytes = out.toByteArray(); + } finally { + instream.close(); + } + return bytes; + } + + @AfterClass + public static void tearDown() throws Exception { + stdIn.write("\n\n".getBytes()); + serverStartThread.join(2000); + Assert.assertFalse(serverStartThread.isAlive()); + } + +} -- cgit v1.2.3 From 350ed420bc91cfa8b6f57478691777fd6499363b Mon Sep 17 00:00:00 2001 From: ritchie Date: Sat, 12 Sep 2015 07:45:31 +0200 Subject: unit tests for nanolets #214 --- .../java/fi/iki/elonen/router/RouterNanoHTTPD.java | 49 +++--- .../java/fi/iki/elonen/router/AppNanolets.java | 28 ++++ .../java/fi/iki/elonen/router/TestNanolets.java | 169 ++++++++++++++++++++- 3 files changed, 221 insertions(+), 25 deletions(-) diff --git a/nanolets/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java b/nanolets/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java index f347741..82dca8b 100644 --- a/nanolets/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java +++ b/nanolets/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java @@ -36,6 +36,7 @@ package fi.iki.elonen.router; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.logging.Level; @@ -223,21 +224,17 @@ public class RouterNanoHTTPD extends NanoHTTPD { } } - public static class UriUtils { - - public static String normalizeUri(String value) { - if (value == null) { - return value; - } - if (value.startsWith("/")) { - value = value.substring(1); - } - if (value.endsWith("/")) { - value = value.substring(0, value.length() - 1); - } + public static String normalizeUri(String value) { + if (value == null) { return value; - } + if (value.startsWith("/")) { + value = value.substring(1); + } + if (value.endsWith("/")) { + value = value.substring(0, value.length() - 1); + } + return value; } @@ -254,7 +251,9 @@ public class RouterNanoHTTPD extends NanoHTTPD { @Override public String toString() { - return "UriPart{name='" + name + '\'' + ", isParam=" + isParam + '}'; + return new StringBuilder("UriPart{name='").append(name)// + .append("\', isParam=").append(isParam)// + .append('}').toString(); } public boolean isParam() { @@ -284,7 +283,7 @@ public class RouterNanoHTTPD extends NanoHTTPD { this.handler = handler; uriParamsCount = 0; if (uri != null) { - this.uri = UriUtils.normalizeUri(uri); + this.uri = normalizeUri(uri); parse(); } } @@ -334,15 +333,11 @@ public class RouterNanoHTTPD extends NanoHTTPD { .append(object)// .toString()); } - } catch (InstantiationException e) { - error = "Error: " + e.getClass().getName() + " : " + e.getMessage(); - LOG.log(Level.SEVERE, error, e); - } catch (IllegalAccessException e) { + } catch (Exception e) { error = "Error: " + e.getClass().getName() + " : " + e.getMessage(); LOG.log(Level.SEVERE, error, e); } } - return NanoHTTPD.newFixedLengthResponse(Status.INTERNAL_ERROR, "text/plain", error); } @@ -397,7 +392,7 @@ public class RouterNanoHTTPD extends NanoHTTPD { * @return */ public UriResource matchUrl(String url) { - String work = UriUtils.normalizeUri(url); + String work = normalizeUri(url); String[] parts = work.split("/"); List resultList = new ArrayList(); for (UriResource u : mappings) { @@ -468,8 +463,14 @@ public class RouterNanoHTTPD extends NanoHTTPD { } public void removeRoute(String url) { - if (mappings.contains(url)) { - mappings.remove(url); + String uriToDelete = normalizeUri(url); + Iterator iter = mappings.iterator(); + while (iter.hasNext()) { + UriResource uriResource = iter.next(); + if (uriToDelete.equals(uriResource.getUri())) { + iter.remove(); + break; + } } } @@ -493,7 +494,7 @@ public class RouterNanoHTTPD extends NanoHTTPD { if (route.getUri() == null) { return result; } - String workUri = UriUtils.normalizeUri(uri); + String workUri = normalizeUri(uri); String[] parts = workUri.split("/"); for (int i = 0; i < parts.length; i++) { UriPart currentPart = route.getUriParts().get(i); diff --git a/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java b/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java index f69a611..b2ae4ed 100644 --- a/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java +++ b/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java @@ -41,9 +41,12 @@ package fi.iki.elonen.router; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import java.util.Map; import fi.iki.elonen.NanoHTTPD; +import fi.iki.elonen.NanoHTTPD.Response.IStatus; +import fi.iki.elonen.NanoHTTPD.Response.Status; import fi.iki.elonen.util.ServerRunner; public class AppNanolets extends RouterNanoHTTPD { @@ -95,6 +98,25 @@ public class AppNanolets extends RouterNanoHTTPD { } + static public class StreamUrl extends DefaultStreamHandler { + + @Override + public String getMimeType() { + return "text/plain"; + } + + @Override + public IStatus getStatus() { + return Status.OK; + } + + @Override + public InputStream getData() { + return new ByteArrayInputStream("a stream of data ;-)".getBytes()); + } + + } + /** * Create the server instance */ @@ -115,8 +137,14 @@ public class AppNanolets extends RouterNanoHTTPD { addRoute("/user", UserHandler.class); addRoute("/user/:id", UserHandler.class); addRoute("/user/help", GeneralHandler.class); + addRoute("/general/:param1/:param2", GeneralHandler.class); addRoute("/photos/:customer_id/:photo_id", null); addRoute("/test", String.class); + addRoute("/interface", UriResponder.class); // this will cause an error + // when called + addRoute("/toBeDeleted", String.class); + removeRoute("/toBeDeleted"); + addRoute("/stream", StreamUrl.class); } /** diff --git a/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java b/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java index 0d35868..bd360da 100644 --- a/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java +++ b/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java @@ -41,7 +41,12 @@ import java.io.PipedOutputStream; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpHead; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpTrace; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.junit.AfterClass; @@ -49,6 +54,9 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import fi.iki.elonen.router.RouterNanoHTTPD.GeneralHandler; +import fi.iki.elonen.router.RouterNanoHTTPD.UriResource; + public class TestNanolets { private static PipedOutputStream stdIn; @@ -73,8 +81,9 @@ public class TestNanolets { } @Test - public void doSomeBasicTest() throws Exception { + public void doSomeBasicMethodTest() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpget = new HttpGet("http://localhost:9090/user/blabla"); CloseableHttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); @@ -82,6 +91,164 @@ public class TestNanolets { Assert.assertEquals( "User handler. Method: GET

Uri parameters:

Param: id Value: blabla

Query parameters:

", string); response.close(); + + HttpPost httppost = new HttpPost("http://localhost:9090/user/blabla"); + response = httpclient.execute(httppost); + entity = response.getEntity(); + string = new String(readContents(entity), "UTF-8"); + Assert.assertEquals( + "User handler. Method: POST

Uri parameters:

Param: id Value: blabla

Query parameters:

", string); + response.close(); + + HttpPut httpgput = new HttpPut("http://localhost:9090/user/blabla"); + response = httpclient.execute(httpgput); + entity = response.getEntity(); + string = new String(readContents(entity), "UTF-8"); + Assert.assertEquals( + "User handler. Method: PUT

Uri parameters:

Param: id Value: blabla

Query parameters:

", string); + response.close(); + + HttpDelete httpdelete = new HttpDelete("http://localhost:9090/user/blabla"); + response = httpclient.execute(httpdelete); + entity = response.getEntity(); + string = new String(readContents(entity), "UTF-8"); + Assert.assertEquals( + "User handler. Method: DELETE

Uri parameters:

Param: id Value: blabla

Query parameters:

", string); + response.close(); + } + + @Test + public void doNonRouterRequest() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + + HttpGet httpget = new HttpGet("http://localhost:9090/test"); + CloseableHttpResponse response = httpclient.execute(httpget); + HttpEntity entity = response.getEntity(); + String string = new String(readContents(entity), "UTF-8"); + Assert.assertEquals("Return: java.lang.String.toString() -> ", string); + response.close(); + } + + @Test + public void doExceptionRequest() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + + HttpGet httpget = new HttpGet("http://localhost:9090/interface"); + CloseableHttpResponse response = httpclient.execute(httpget); + HttpEntity entity = response.getEntity(); + String string = new String(readContents(entity), "UTF-8"); + Assert.assertEquals("Error: java.lang.InstantiationException : fi.iki.elonen.router.RouterNanoHTTPD$UriResponder", string); + response.close(); + } + + @Test + public void doDeletedRoute() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + + HttpGet httpget = new HttpGet("http://localhost:9090/toBeDeleted"); + CloseableHttpResponse response = httpclient.execute(httpget); + HttpEntity entity = response.getEntity(); + String string = new String(readContents(entity), "UTF-8"); + Assert.assertEquals("

Error 404: the requested page doesn't exist.

", string); + response.close(); + } + + @Test + public void doUriSelection1() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + + HttpGet httpget = new HttpGet("http://localhost:9090/user/help"); + CloseableHttpResponse response = httpclient.execute(httpget); + HttpEntity entity = response.getEntity(); + String string = new String(readContents(entity), "UTF-8"); + Assert.assertEquals("

Url: /user/help


no params in url


", string); + response.close(); + } + + @Test + public void doStreamOfData() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + + HttpGet httpget = new HttpGet("http://localhost:9090/stream"); + CloseableHttpResponse response = httpclient.execute(httpget); + HttpEntity entity = response.getEntity(); + String string = new String(readContents(entity), "UTF-8"); + Assert.assertEquals("a stream of data ;-)", string); + response.close(); + } + + @Test(expected = IllegalStateException.class) + public void illegalMethod1() throws Exception { + new AppNanolets.UserHandler().getData(); + } + + @Test(expected = IllegalStateException.class) + public void illegalMethod2() throws Exception { + new RouterNanoHTTPD.GeneralHandler().getText(); + } + + @Test + public void doGeneralParams() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + + HttpGet httpget = new HttpGet("http://localhost:9090/general/value1/value2?param3=value3¶m4=value4"); + + CloseableHttpResponse response = httpclient.execute(httpget); + HttpEntity entity = response.getEntity(); + String string = new String(readContents(entity), "UTF-8"); + Assert.assertEquals("

Url: /general/value1/value2


Param 'param3' = value3

Param 'param4' = value4

", string); + response.close(); + } + + @Test + public void doIndexHandler() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + + HttpGet httpget = new HttpGet("http://localhost:9090/index.html"); + CloseableHttpResponse response = httpclient.execute(httpget); + HttpEntity entity = response.getEntity(); + String string = new String(readContents(entity), "UTF-8"); + Assert.assertEquals("

Hello world!

", string); + response.close(); + } + + @Test + public void doMissingHandler() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + + HttpGet httpget = new HttpGet("http://localhost:9090/photos/abc/def"); + CloseableHttpResponse response = httpclient.execute(httpget); + HttpEntity entity = response.getEntity(); + String string = new String(readContents(entity), "UTF-8"); + Assert.assertEquals("

The uri is mapped in the router, but no handler is specified.
Status: Not implemented!

", string); + response.close(); + } + + @Test + public void uriToString() throws Exception { + Assert.assertEquals(// + "UrlResource{hasParameters=true, uriParamsCount=2, uri='photos/:customer_id/:photo_id', urlParts=[UriPart{name='photos', isParam=false}, UriPart{name='customer_id', isParam=true}, UriPart{name='photo_id', isParam=true}]}",// + new UriResource("/photos/:customer_id/:photo_id", GeneralHandler.class).toString()); + } + + @Test + public void doOtherMethod() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + + HttpTrace httphead = new HttpTrace("http://localhost:9090/index.html"); + CloseableHttpResponse response = httpclient.execute(httphead); + HttpEntity entity = response.getEntity(); + String string = new String(readContents(entity), "UTF-8"); + Assert.assertEquals("

Hello world!

", string); + response.close(); + } + + @Test + public void normalize() throws Exception { + Assert.assertNull(RouterNanoHTTPD.normalizeUri(null)); + Assert.assertEquals("", RouterNanoHTTPD.normalizeUri("/")); + Assert.assertEquals("xxx/yyy", RouterNanoHTTPD.normalizeUri("/xxx/yyy")); + Assert.assertEquals("xxx/yyy", RouterNanoHTTPD.normalizeUri("/xxx/yyy/")); } private byte[] readContents(HttpEntity entity) throws IOException { -- cgit v1.2.3 From cebdea5194b468d09fa78f69c4b234c9a7acb080 Mon Sep 17 00:00:00 2001 From: ritchie Date: Sat, 12 Sep 2015 09:13:26 +0200 Subject: apache file upload integration #216 #219 (unit test not acive yet) --- fileupload/pom.xml | 78 ++++----- .../main/java/fi/iki/elonen/NanoFileUpload.java | 178 ++++++++++++--------- .../java/fi/iki/elonen/TestNanoFileUpLoad.java | 172 ++++++++++++++++++++ nanolets/pom.xml | 2 +- 4 files changed, 319 insertions(+), 111 deletions(-) create mode 100644 fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java diff --git a/fileupload/pom.xml b/fileupload/pom.xml index da60ecf..2ed65d3 100644 --- a/fileupload/pom.xml +++ b/fileupload/pom.xml @@ -1,39 +1,43 @@ - - - - nanohttpd-project - org.nanohttpd - 2.2.0-SNAPSHOT - - 4.0.0 - - apache-fileupload-integration - - - org.nanohttpd - nanohttpd - 2.2.0-SNAPSHOT - provided - - - - commons-fileupload - commons-fileupload - 1.3.1 - provided - - - - - javax.servlet - servlet-api - 2.5 - provided - - - - + + + nanohttpd-project + org.nanohttpd + 2.2.0-SNAPSHOT + + 4.0.0 + nanohttpd-apache-fileupload + NanoHttpd-apache file upload integration + + + org.nanohttpd + nanohttpd + 2.2.0-SNAPSHOT + provided + + + commons-fileupload + commons-fileupload + 1.3.1 + + + javax.servlet + servlet-api + 2.5 + provided + + + org.apache.httpcomponents + httpclient + 4.4.1 + test + + + org.apache.httpcomponents + httpmime + 4.4.1 + test + + \ No newline at end of file diff --git a/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java b/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java index f51df3c..6dd06d5 100644 --- a/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java +++ b/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java @@ -1,5 +1,45 @@ package fi.iki.elonen; +/* + * #%L + * apache-fileupload-integration + * %% + * 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 static fi.iki.elonen.NanoHTTPD.Method.POST; + +import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import java.util.Map; + import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileItemIterator; @@ -8,82 +48,74 @@ import org.apache.commons.fileupload.FileUploadBase; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.UploadContext; -import java.io.IOException; -import java.io.InputStream; -import java.util.List; -import java.util.Map; - /** - * Created by victor on 7/30/15. + * @author victor & ritchieGitHub */ public class NanoFileUpload extends FileUpload { - public static class NanoHttpdContext implements UploadContext { - - private NanoHTTPD.IHTTPSession session; - - public NanoHttpdContext(NanoHTTPD.IHTTPSession session) { - this.session = session; - } - - @Override - public long contentLength() { - long size; - try { - String cl1 = session.getHeaders().get("content-length"); - size = Long.parseLong(cl1); - } catch (NumberFormatException var4) { - size = -1L; - } - - return size; - } - - @Override - public String getCharacterEncoding() { - return "UTF-8"; - } - - @Override - public String getContentType() { - return this.session.getHeaders().get("content-type"); - } - - @Override - public int getContentLength() { - return (int)contentLength(); - } - - @Override - public InputStream getInputStream() throws IOException { - return session.getInputStream(); - } - } - - private static final String POST_METHOD = "POST"; - - public static final boolean isMultipartContent(NanoHTTPD.IHTTPSession session) { - return !"POST".equalsIgnoreCase(session.getMethod().toString()) - ? false: FileUploadBase.isMultipartContent(new NanoHttpdContext(session)); - } - - public NanoFileUpload() { - } - - public NanoFileUpload(FileItemFactory fileItemFactory) { - super(fileItemFactory); - } - - public List parseRequest(NanoHTTPD.IHTTPSession session) throws FileUploadException { - return this.parseRequest(new NanoHttpdContext(session)); - } - - public Map> parseParameterMap(NanoHTTPD.IHTTPSession session) throws FileUploadException { - return this.parseParameterMap(new NanoHttpdContext(session)); - } - - public FileItemIterator getItemIterator(NanoHTTPD.IHTTPSession session) throws FileUploadException, IOException { - return super.getItemIterator(new NanoHttpdContext(session)); - } + public static class NanoHttpdContext implements UploadContext { + + private NanoHTTPD.IHTTPSession session; + + public NanoHttpdContext(NanoHTTPD.IHTTPSession session) { + this.session = session; + } + + @Override + public long contentLength() { + long size; + try { + String cl1 = session.getHeaders().get("content-length"); + size = Long.parseLong(cl1); + } catch (NumberFormatException var4) { + size = -1L; + } + + return size; + } + + @Override + public String getCharacterEncoding() { + return "UTF-8"; + } + + @Override + public String getContentType() { + return this.session.getHeaders().get("content-type"); + } + + @Override + public int getContentLength() { + return (int) contentLength(); + } + + @Override + public InputStream getInputStream() throws IOException { + return session.getInputStream(); + } + } + + public static final boolean isMultipartContent(NanoHTTPD.IHTTPSession session) { + return session.getMethod() == POST && FileUploadBase.isMultipartContent(new NanoHttpdContext(session)); + } + + public NanoFileUpload() { + } + + public NanoFileUpload(FileItemFactory fileItemFactory) { + super(fileItemFactory); + } + + public List parseRequest(NanoHTTPD.IHTTPSession session) throws FileUploadException { + return this.parseRequest(new NanoHttpdContext(session)); + } + + public Map> parseParameterMap(NanoHTTPD.IHTTPSession session) throws FileUploadException { + return this.parseParameterMap(new NanoHttpdContext(session)); + } + + public FileItemIterator getItemIterator(NanoHTTPD.IHTTPSession session) throws FileUploadException, IOException { + return super.getItemIterator(new NanoHttpdContext(session)); + } } diff --git a/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java b/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java new file mode 100644 index 0000000..558ab01 --- /dev/null +++ b/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java @@ -0,0 +1,172 @@ +package fi.iki.elonen; + +/* + * #%L + * NanoHttpd-apache file upload integration + * %% + * 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 java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.util.List; +import java.util.Map; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileUploadException; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpTrace; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.entity.mime.content.FileBody; +import org.apache.http.entity.mime.content.StringBody; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +public class TestNanoFileUpLoad { + + protected TestServer testServer; + + public static class TestServer extends NanoHTTPD { + + public Response response = newFixedLengthResponse(""); + + public String uri; + + public Method method; + + public Map header; + + public Map parms; + + public Map> files; + + public Map> decodedParamters; + + public Map> decodedParamtersFromParameter; + + public String queryParameterString; + + public TestServer() { + super(8192); + uploader = new NanoFileUpload(); + uploader.setFileItemFactory(new DiskFileItemFactory()); + } + + public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream) { + return new HTTPSession(tempFileManager, inputStream, outputStream); + } + + public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) { + return new HTTPSession(tempFileManager, inputStream, outputStream, inetAddress); + } + + @Override + public String decodePercent(String str) { + return super.decodePercent(str); + } + + NanoFileUpload uploader; + + @Override + public Response serve(IHTTPSession session) { + + this.uri = session.getUri(); + this.method = session.getMethod(); + this.header = session.getHeaders(); + this.parms = session.getParms(); + if (NanoFileUpload.isMultipartContent(session)) { + try { + files = uploader.parseParameterMap(session); + + } catch (FileUploadException e) { + e.printStackTrace(); + } + } + this.queryParameterString = session.getQueryParameterString(); + this.decodedParamtersFromParameter = decodeParameters(this.queryParameterString); + this.decodedParamters = decodeParameters(session.getQueryParameterString()); + return this.response; + } + + } + + @Test + public void testNormalRequest() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpTrace httphead = new HttpTrace("http://localhost:8192/index.html"); + CloseableHttpResponse response = httpclient.execute(httphead); + response.close(); + + } + + @Ignore + @Test + public void testPostWithMultipartFormUpload() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + String textFileName = "src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java"; + HttpPost post = new HttpPost("http://localhost:8192/uploadFile"); + FileBody fileBody = new FileBody(new File(textFileName), ContentType.DEFAULT_BINARY); + StringBody stringBody1 = new StringBody("Message 1", ContentType.MULTIPART_FORM_DATA); + + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + builder.addPart("upfile", fileBody); + builder.addPart("text1", stringBody1); + HttpEntity entity = builder.build(); + // + post.setEntity(entity); + HttpResponse response = httpclient.execute(post); + response.toString(); + } + + @Before + public void setUp() throws IOException { + this.testServer = new TestServer(); + this.testServer.start(); + } + + @After + public void tearDown() { + this.testServer.stop(); + } + +} diff --git a/nanolets/pom.xml b/nanolets/pom.xml index 39af427..f6d3239 100644 --- a/nanolets/pom.xml +++ b/nanolets/pom.xml @@ -8,7 +8,7 @@ nanohttpd-nanolets jar - NanoHttpd nano application server + NanoHttpd-nano application server ${project.groupId} -- 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 +++ fileupload/.gitignore | 2 ++ fileupload/pom.xml | 3 +++ .../main/java/fi/iki/elonen/NanoFileUpload.java | 3 --- .../java/fi/iki/elonen/TestNanoFileUpLoad.java | 7 ++++--- markdown-plugin/pom.xml | 3 +++ nanolets/pom.xml | 3 +++ pom.xml | 23 ++++++++++++++++++++++ samples/pom.xml | 3 +++ webserver/pom.xml | 3 +++ websocket/pom.xml | 3 +++ .../java/fi/iki/elonen/NanoWebSocketServer.java | 12 +++-------- .../elonen/samples/echo/EchoWebSocketsTest.java | 12 ++++++++++- 13 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 fileupload/.gitignore 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 + diff --git a/fileupload/.gitignore b/fileupload/.gitignore new file mode 100644 index 0000000..868a6b2 --- /dev/null +++ b/fileupload/.gitignore @@ -0,0 +1,2 @@ +/.settings/ +/LICENSE.txt diff --git a/fileupload/pom.xml b/fileupload/pom.xml index 2ed65d3..ebd9e9e 100644 --- a/fileupload/pom.xml +++ b/fileupload/pom.xml @@ -40,4 +40,7 @@ test + + 0.1 + \ No newline at end of file diff --git a/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java b/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java index 6dd06d5..ec02d4a 100644 --- a/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java +++ b/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java @@ -99,9 +99,6 @@ public class NanoFileUpload extends FileUpload { return session.getMethod() == POST && FileUploadBase.isMultipartContent(new NanoHttpdContext(session)); } - public NanoFileUpload() { - } - public NanoFileUpload(FileItemFactory fileItemFactory) { super(fileItemFactory); } diff --git a/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java b/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java index 558ab01..f0d72e2 100644 --- a/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java +++ b/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java @@ -61,6 +61,8 @@ import org.junit.Before; import org.junit.Ignore; import org.junit.Test; +import fi.iki.elonen.NanoHTTPD.Response.Status; + public class TestNanoFileUpLoad { protected TestServer testServer; @@ -87,8 +89,7 @@ public class TestNanoFileUpLoad { public TestServer() { super(8192); - uploader = new NanoFileUpload(); - uploader.setFileItemFactory(new DiskFileItemFactory()); + uploader = new NanoFileUpload(new DiskFileItemFactory()); } public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream) { @@ -116,8 +117,8 @@ public class TestNanoFileUpLoad { if (NanoFileUpload.isMultipartContent(session)) { try { files = uploader.parseParameterMap(session); - } catch (FileUploadException e) { + this.response.setStatus(Status.INTERNAL_ERROR); e.printStackTrace(); } } diff --git a/markdown-plugin/pom.xml b/markdown-plugin/pom.xml index e13b042..6b7ad7a 100644 --- a/markdown-plugin/pom.xml +++ b/markdown-plugin/pom.xml @@ -56,4 +56,7 @@ + + 0.0 + diff --git a/nanolets/pom.xml b/nanolets/pom.xml index f6d3239..a190abb 100644 --- a/nanolets/pom.xml +++ b/nanolets/pom.xml @@ -22,4 +22,7 @@ test + + 0.98 + diff --git a/pom.xml b/pom.xml index 91d1397..7e9deea 100644 --- a/pom.xml +++ b/pom.xml @@ -233,6 +233,26 @@ report + + default-check + + check + + + + + BUNDLE + + + LINE + COVEREDRATIO + ${minimal.coverage} + + + + + + @@ -475,4 +495,7 @@ + + 0.76 + diff --git a/samples/pom.xml b/samples/pom.xml index 620349c..af27870 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -20,4 +20,7 @@ ${project.version} + + 0.0 + diff --git a/webserver/pom.xml b/webserver/pom.xml index 45f726b..3ecfd72 100644 --- a/webserver/pom.xml +++ b/webserver/pom.xml @@ -49,4 +49,7 @@ + + 0.74 + diff --git a/websocket/pom.xml b/websocket/pom.xml index 8c7aee4..9b7fd2c 100644 --- a/websocket/pom.xml +++ b/websocket/pom.xml @@ -62,4 +62,7 @@ test + + 0.67 + diff --git a/websocket/src/main/java/fi/iki/elonen/NanoWebSocketServer.java b/websocket/src/main/java/fi/iki/elonen/NanoWebSocketServer.java index e23abe2..73e5f67 100644 --- a/websocket/src/main/java/fi/iki/elonen/NanoWebSocketServer.java +++ b/websocket/src/main/java/fi/iki/elonen/NanoWebSocketServer.java @@ -96,13 +96,13 @@ public abstract class NanoWebSocketServer extends NanoHTTPD { this.handshakeResponse.addHeader(NanoWebSocketServer.HEADER_CONNECTION, NanoWebSocketServer.HEADER_CONNECTION_VALUE); } - public void close(CloseCode code, String reason) throws IOException { + public void close(CloseCode code, String reason, boolean initiatedByRemote) throws IOException { State oldState = this.state; this.state = State.CLOSING; if (oldState == State.OPEN) { sendFrame(new CloseFrame(code, reason)); } else { - doClose(code, reason, false); + doClose(code, reason, initiatedByRemote); } } @@ -149,13 +149,7 @@ public abstract class NanoWebSocketServer extends NanoHTTPD { // Answer for my requested close doClose(code, reason, false); } else { - // Answer close request from other endpoint and close self - State oldState = this.state; - this.state = State.CLOSING; - if (oldState == State.OPEN) { - sendFrame(new CloseFrame(code, reason)); - } - doClose(code, reason, true); + close(code, reason, true); } } diff --git a/websocket/src/test/java/fi/iki/elonen/samples/echo/EchoWebSocketsTest.java b/websocket/src/test/java/fi/iki/elonen/samples/echo/EchoWebSocketsTest.java index 6061023..e90ae71 100644 --- a/websocket/src/test/java/fi/iki/elonen/samples/echo/EchoWebSocketsTest.java +++ b/websocket/src/test/java/fi/iki/elonen/samples/echo/EchoWebSocketsTest.java @@ -68,6 +68,8 @@ public class EchoWebSocketsTest { SimpleEchoSocket socket = new SimpleEchoSocket(); socket.getToSendMessages().add("Hello"); socket.getToSendMessages().add("Thanks for the conversation."); + socket.getToSendMessages().add(createString(31000)); + socket.getToSendMessages().add(createString(65400)); try { client.start(); URI echoUri = new URI(destUri); @@ -84,9 +86,17 @@ public class EchoWebSocketsTest { e.printStackTrace(); } } - Assert.assertEquals(2, socket.getReceivedMessages().size()); + Assert.assertEquals(4, socket.getReceivedMessages().size()); Assert.assertEquals("Hello", socket.getReceivedMessages().get(0)); Assert.assertEquals("Thanks for the conversation.", socket.getReceivedMessages().get(1)); } + + private String createString(int i) { + StringBuilder builder = new StringBuilder(); + while (builder.length() < i) { + builder.append("A very long text."); + } + return builder.toString(); + } } -- cgit v1.2.3 From 52e133eaffa4906f4d85ec1374c99ee475ead36d Mon Sep 17 00:00:00 2001 From: ritchie Date: Sun, 13 Sep 2015 06:59:56 +0200 Subject: unit tests for all cases #219 #216 --- fileupload/pom.xml | 2 +- .../java/fi/iki/elonen/TestNanoFileUpLoad.java | 92 ++++++++++++++++++++-- pom.xml | 2 +- 3 files changed, 87 insertions(+), 9 deletions(-) diff --git a/fileupload/pom.xml b/fileupload/pom.xml index ebd9e9e..0be88a9 100644 --- a/fileupload/pom.xml +++ b/fileupload/pom.xml @@ -41,6 +41,6 @@ - 0.1 + 0.99 \ No newline at end of file diff --git a/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java b/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java index f0d72e2..f04375d 100644 --- a/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java +++ b/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java @@ -38,14 +38,20 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileItemIterator; +import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.commons.fileupload.util.Streams; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpTrace; @@ -57,12 +63,20 @@ import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.junit.After; +import org.junit.Assert; import org.junit.Before; -import org.junit.Ignore; +import org.junit.FixMethodOrder; import org.junit.Test; +import org.junit.internal.runners.statements.Fail; import fi.iki.elonen.NanoHTTPD.Response.Status; +/** + * very strange but if the file upload is the first request the test fails. + * + * @author ritchieGitHub + */ +@FixMethodOrder public class TestNanoFileUpLoad { protected TestServer testServer; @@ -116,8 +130,33 @@ public class TestNanoFileUpLoad { this.parms = session.getParms(); if (NanoFileUpload.isMultipartContent(session)) { try { - files = uploader.parseParameterMap(session); - } catch (FileUploadException e) { + if ("/uploadFile1".equals(this.uri)) { + session.getHeaders().put("content-length", "AA"); + files = uploader.parseParameterMap(session); + } + if ("/uploadFile2".equals(this.uri)) { + files = new HashMap>(); + List parseRequest = uploader.parseRequest(session); + files.put(parseRequest.get(0).getFieldName(), parseRequest); + } + if ("/uploadFile3".equals(this.uri)) { + files = new HashMap>(); + FileItemIterator iter = uploader.getItemIterator(session); + while (iter.hasNext()) { + FileItemStream item = iter.next(); + final String fileName = item.getName(); + FileItem fileItem = uploader.getFileItemFactory().createItem(item.getFieldName(), item.getContentType(), item.isFormField(), fileName); + files.put(fileItem.getFieldName(), Arrays.asList(new FileItem[]{ + fileItem + })); + try { + Streams.copy(item.openStream(), fileItem.getOutputStream(), true); + } catch (Exception e) { + } + fileItem.setHeaders(item.getHeaders()); + } + } + } catch (Exception e) { this.response.setStatus(Status.INTERNAL_ERROR); e.printStackTrace(); } @@ -135,16 +174,44 @@ public class TestNanoFileUpLoad { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpTrace httphead = new HttpTrace("http://localhost:8192/index.html"); CloseableHttpResponse response = httpclient.execute(httphead); + Assert.assertEquals(200, response.getStatusLine().getStatusCode()); response.close(); + } + + @Test + public void testPostWithMultipartFormUpload1() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + String textFileName = "src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java"; + HttpPost post = new HttpPost("http://localhost:8192/uploadFile1"); + executeUpload(httpclient, textFileName, post); + FileItem file = this.testServer.files.get("upfile").get(0); + Assert.assertEquals(file.getSize(), new File(textFileName).length()); } - @Ignore @Test - public void testPostWithMultipartFormUpload() throws Exception { + public void testPostWithMultipartFormUpload2() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); String textFileName = "src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java"; - HttpPost post = new HttpPost("http://localhost:8192/uploadFile"); + HttpPost post = new HttpPost("http://localhost:8192/uploadFile2"); + + executeUpload(httpclient, textFileName, post); + FileItem file = this.testServer.files.get("upfile").get(0); + Assert.assertEquals(file.getSize(), new File(textFileName).length()); + } + + @Test + public void testPostWithMultipartFormUpload3() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + String textFileName = "src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java"; + HttpPost post = new HttpPost("http://localhost:8192/uploadFile3"); + + executeUpload(httpclient, textFileName, post); + FileItem file = this.testServer.files.get("upfile").get(0); + Assert.assertEquals(file.getSize(), new File(textFileName).length()); + } + + private void executeUpload(CloseableHttpClient httpclient, String textFileName, HttpPost post) throws IOException, ClientProtocolException { FileBody fileBody = new FileBody(new File(textFileName), ContentType.DEFAULT_BINARY); StringBody stringBody1 = new StringBody("Message 1", ContentType.MULTIPART_FORM_DATA); @@ -156,13 +223,24 @@ public class TestNanoFileUpLoad { // post.setEntity(entity); HttpResponse response = httpclient.execute(post); - response.toString(); + Assert.assertEquals(200, response.getStatusLine().getStatusCode()); } @Before public void setUp() throws IOException { this.testServer = new TestServer(); 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 diff --git a/pom.xml b/pom.xml index 7e9deea..6be54b7 100644 --- a/pom.xml +++ b/pom.xml @@ -340,7 +340,7 @@ junit junit - 4.8.2 + 4.12 test -- cgit v1.2.3 From b7c544cefdb659bc78bfc57fd505fd45b5f1d1ff Mon Sep 17 00:00:00 2001 From: ritchie Date: Sun, 13 Sep 2015 08:06:09 +0200 Subject: increase minimal coverage --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6be54b7..9334315 100644 --- a/pom.xml +++ b/pom.xml @@ -496,6 +496,6 @@ - 0.76 + 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 --- README.md | 12 +++ 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 8 files changed, 128 insertions(+), 24 deletions(-) create mode 100644 core/src/test/java/fi/iki/elonen/HttpSSLServerTest.java create mode 100644 core/src/test/resources/keystore.jks diff --git a/README.md b/README.md index e1f5484..e7835a0 100644 --- a/README.md +++ b/README.md @@ -211,7 +211,19 @@ The latest Github master version can be fetched through sonatype.org: +### generating an self signed ssl certificate +Just a hint how to generate a certificate for localhost. + + keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048 -ext SAN=DNS:localhost,IP:127.0.0.1 -validity 9999 + +This will generate a keystore file named 'keystore.jks' with a self signed certificate for a host named localhost with the ip adress 127.0.0.1 . Now +you can use: + + server.makeSecure(NanoHTTPD.makeSSLSocketFactory("/keystore.jks", "password".toCharArray())); + +Before you start the server to make Nanohttpd serve https connections, when you make sure 'keystore.jks' is in your classpath . + ----- *Thank you to everyone who has reported bugs and suggested fixes.* 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(-) 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(-) 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