17cb7c97c057d57ce4147b1ae0bec82c092f2851aJarno Elonen## NanoHTTPD – a tiny web server in Java
203b577b50536e29b78df57f7107996ad01eaa32elonen
37cb7c97c057d57ce4147b1ae0bec82c092f2851aJarno Elonen*NanoHTTPD* is a light-weight HTTP server designed for embedding in other applications, released under a Modified BSD licence.
4afd3338fe6e7e2179ee4ffbaa409c8692b4346ccPaul Hawke
57cb7c97c057d57ce4147b1ae0bec82c092f2851aJarno ElonenIt is being developed at Github and uses Apache Maven for builds & unit testing:
6afd3338fe6e7e2179ee4ffbaa409c8692b4346ccPaul Hawke
77cb7c97c057d57ce4147b1ae0bec82c092f2851aJarno Elonen * Build status: [![Build Status](https://api.travis-ci.org/NanoHttpd/nanohttpd.png)](https://travis-ci.org/NanoHttpd/nanohttpd)
87cb7c97c057d57ce4147b1ae0bec82c092f2851aJarno Elonen * Coverage Status: [![Coverage Status](https://coveralls.io/repos/NanoHttpd/nanohttpd/badge.svg)](https://coveralls.io/r/NanoHttpd/nanohttpd)
97cb7c97c057d57ce4147b1ae0bec82c092f2851aJarno Elonen * Current central released version: [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.nanohttpd/nanohttpd/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.nanohttpd/nanohttpd)
1063fa9f32ea5c4311898eb1628b0daa58c00e18f4ritchie
1162d6807a93f115642f50732b77b0b03565862e05Jarno Elonen## Quickstart
129bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie
1362d6807a93f115642f50732b77b0b03565862e05Jarno ElonenWe'll create a custom HTTP server project using Maven for build/dep system. This tutorial assumes you are using a Unix variant and a shell. First, install Maven and Java SDK if not already installed. Then run:
1462d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
159d3564a23d413861deeb3996f93bcc5dc5aa7827elonen    mvn compile
169d3564a23d413861deeb3996f93bcc5dc5aa7827elonen    mvn exec:java -pl webserver -Dexec.mainClass="fi.iki.elonen.SimpleWebServer"
179d3564a23d413861deeb3996f93bcc5dc5aa7827elonen    
189d3564a23d413861deeb3996f93bcc5dc5aa7827elonenYou should now have a HTTP file server running on <http://localhost:8080/>.
199d3564a23d413861deeb3996f93bcc5dc5aa7827elonen
209d3564a23d413861deeb3996f93bcc5dc5aa7827elonen### Custom web app
219d3564a23d413861deeb3996f93bcc5dc5aa7827elonen
229d3564a23d413861deeb3996f93bcc5dc5aa7827elonenLet's raise the bar and build a custom web application next:
239d3564a23d413861deeb3996f93bcc5dc5aa7827elonen
2462d6807a93f115642f50732b77b0b03565862e05Jarno Elonen    mvn archetype:generate -DgroupId=com.example -DartifactId=myHellopApp -DinteractiveMode=false
2562d6807a93f115642f50732b77b0b03565862e05Jarno Elonen    cd myHellopApp
2662d6807a93f115642f50732b77b0b03565862e05Jarno Elonen    
2762d6807a93f115642f50732b77b0b03565862e05Jarno ElonenEdit `pom.xml`, and add this between \<dependencies\>:
2862d6807a93f115642f50732b77b0b03565862e05Jarno Elonen 
2962d6807a93f115642f50732b77b0b03565862e05Jarno Elonen	<dependency>
3034ea556fbb94657ff8be493b937f5e5e75ed0f28ritchie		<groupId>org.nanohttpd</groupId> <!-- <groupId>com.nanohttpd</groupId> for 2.1.0 and earlier -->
3162d6807a93f115642f50732b77b0b03565862e05Jarno Elonen		<artifactId>nanohttpd</artifactId>
3262d6807a93f115642f50732b77b0b03565862e05Jarno Elonen		<version>2.2.0-SNAPSHOT</version>
3362d6807a93f115642f50732b77b0b03565862e05Jarno Elonen	</dependency>
342b3d434d8f056d4284a10b441fe1d6998d96ff3eritchie	
3562d6807a93f115642f50732b77b0b03565862e05Jarno ElonenEdit `src/main/java/com/example/App.java` and replace it with:
366b1b0e8f08972ca654ac49e8e899583362fce935Jarno Elonen```java
37582a893592ac2bd2360d753393421dd0680067cfJarno Elonenpackage com.example;
38582a893592ac2bd2360d753393421dd0680067cfJarno Elonen
39582a893592ac2bd2360d753393421dd0680067cfJarno Elonenimport java.util.Map;
40582a893592ac2bd2360d753393421dd0680067cfJarno Elonenimport java.io.IOException;
41582a893592ac2bd2360d753393421dd0680067cfJarno Elonenimport fi.iki.elonen.NanoHTTPD;
42582a893592ac2bd2360d753393421dd0680067cfJarno Elonen
43582a893592ac2bd2360d753393421dd0680067cfJarno Elonenpublic class App extends NanoHTTPD {
44582a893592ac2bd2360d753393421dd0680067cfJarno Elonen
45582a893592ac2bd2360d753393421dd0680067cfJarno Elonen    public App() throws IOException {
46582a893592ac2bd2360d753393421dd0680067cfJarno Elonen        super(8080);
47582a893592ac2bd2360d753393421dd0680067cfJarno Elonen        start();
48582a893592ac2bd2360d753393421dd0680067cfJarno Elonen		System.out.println( "\nRunning! Point your browers to http://localhost:8080/ \n" );
49582a893592ac2bd2360d753393421dd0680067cfJarno Elonen    }
50582a893592ac2bd2360d753393421dd0680067cfJarno Elonen
51582a893592ac2bd2360d753393421dd0680067cfJarno Elonen    public static void main(String[] args) {
52582a893592ac2bd2360d753393421dd0680067cfJarno Elonen		try {
53582a893592ac2bd2360d753393421dd0680067cfJarno Elonen		    new App();
54582a893592ac2bd2360d753393421dd0680067cfJarno Elonen		}
55582a893592ac2bd2360d753393421dd0680067cfJarno Elonen		catch( IOException ioe ) {
56582a893592ac2bd2360d753393421dd0680067cfJarno Elonen			System.err.println( "Couldn't start server:\n" + ioe );
57582a893592ac2bd2360d753393421dd0680067cfJarno Elonen		}
58582a893592ac2bd2360d753393421dd0680067cfJarno Elonen    }
59582a893592ac2bd2360d753393421dd0680067cfJarno Elonen
60582a893592ac2bd2360d753393421dd0680067cfJarno Elonen    @Override
61582a893592ac2bd2360d753393421dd0680067cfJarno Elonen    public Response serve(IHTTPSession session) {
62582a893592ac2bd2360d753393421dd0680067cfJarno Elonen        String msg = "<html><body><h1>Hello server</h1>\n";
63582a893592ac2bd2360d753393421dd0680067cfJarno Elonen        Map<String, String> parms = session.getParms();
64582a893592ac2bd2360d753393421dd0680067cfJarno Elonen        if (parms.get("username") == null) {
65582a893592ac2bd2360d753393421dd0680067cfJarno Elonen            msg += "<form action='?' method='get'>\n  <p>Your name: <input type='text' name='username'></p>\n" + "</form>\n";
66582a893592ac2bd2360d753393421dd0680067cfJarno Elonen        } else {
67582a893592ac2bd2360d753393421dd0680067cfJarno Elonen            msg += "<p>Hello, " + parms.get("username") + "!</p>";
68582a893592ac2bd2360d753393421dd0680067cfJarno Elonen        }
69582a893592ac2bd2360d753393421dd0680067cfJarno Elonen        return newFixedLengthResponse( msg + "</body></html>\n" );
70582a893592ac2bd2360d753393421dd0680067cfJarno Elonen    }
71582a893592ac2bd2360d753393421dd0680067cfJarno Elonen}
726b1b0e8f08972ca654ac49e8e899583362fce935Jarno Elonen```
7362d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
7462d6807a93f115642f50732b77b0b03565862e05Jarno ElonenCompile and run the server:
7562d6807a93f115642f50732b77b0b03565862e05Jarno Elonen 
7662d6807a93f115642f50732b77b0b03565862e05Jarno Elonen    mvn compile
7762d6807a93f115642f50732b77b0b03565862e05Jarno Elonen    mvn exec:java -Dexec.mainClass="com.example.App"
7862d6807a93f115642f50732b77b0b03565862e05Jarno Elonen    
7962d6807a93f115642f50732b77b0b03565862e05Jarno ElonenIf it started ok, point your browser at <http://localhost:8080/> and enjoy a web server that asks your name and replies with a greeting. 
8062d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
81ed02162dc4b43886e23b2b6888d03bf1f3801d6eritchie### Nanolets
82ed02162dc4b43886e23b2b6888d03bf1f3801d6eritchie
83ed02162dc4b43886e23b2b6888d03bf1f3801d6eritchieNanolets are like sevlet's only that they have a extrem low profile. They offer an easy to use system for a more complex server application. 
84ed02162dc4b43886e23b2b6888d03bf1f3801d6eritchieThis text has to be extrended with an example, so for now take a look at the unit tests for the usage. <https://github.com/NanoHttpd/nanohttpd/blob/master/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java>
85ed02162dc4b43886e23b2b6888d03bf1f3801d6eritchie
8662d6807a93f115642f50732b77b0b03565862e05Jarno Elonen## Status
8762d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
8862d6807a93f115642f50732b77b0b03565862e05Jarno ElonenWe are currently in the process of stabilizing NanoHttpd from the many pull requests and feature requests that were integrated over the last few months. The next release will come soon, and there will not be any more "intended" major changes before the next release. If you want to use the bleeding edge version, you can clone it from Github, or get it from sonatype.org (see "Maven dependencies / Living on the edge" below).
8962d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
9062d6807a93f115642f50732b77b0b03565862e05Jarno Elonen## Project structure
9162d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
9262d6807a93f115642f50732b77b0b03565862e05Jarno ElonenNanoHTTPD project currently consist of four parts:
9362d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
9462d6807a93f115642f50732b77b0b03565862e05Jarno Elonen * `/core` – Fully functional HTTP(s) server consisting of one (1) Java file, ready to be customized/inherited for your own project
9562d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
9662d6807a93f115642f50732b77b0b03565862e05Jarno Elonen * `/samples` – Simple examples on how to customize NanoHTTPD. See *HelloServer.java* for a killer app that greets you enthusiastically!
9762d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
9862d6807a93f115642f50732b77b0b03565862e05Jarno Elonen * `/websocket` – Websocket implementation, also in a single Java file. Depends on core.
9962d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
10062d6807a93f115642f50732b77b0b03565862e05Jarno Elonen * `/webserver` – Standalone file server. Run & enjoy. A popular use seems to be serving files out off an Android device.
10162d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
102ed02162dc4b43886e23b2b6888d03bf1f3801d6eritchie * `/nanolets` – Standalone nano app server, giving a servlet like system to the implementor.
103ed02162dc4b43886e23b2b6888d03bf1f3801d6eritchie
104ed02162dc4b43886e23b2b6888d03bf1f3801d6eritchie * `/fileupload` – integration of the apache common file upload library.
105ed02162dc4b43886e23b2b6888d03bf1f3801d6eritchie
10662d6807a93f115642f50732b77b0b03565862e05Jarno Elonen## Features
10762d6807a93f115642f50732b77b0b03565862e05Jarno Elonen### Core
10862d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* Only one Java file, providing HTTP 1.1 support.
10962d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* No fixed config files, logging, authorization etc. (Implement by yourself if you need them. Errors are passed to java.util.logging, though.)
11062d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* Support for HTTPS (SSL)
11162d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* Basic support for cookies
11262d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* Supports parameter parsing of GET and POST methods.
11362d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* Some built-in support for HEAD, POST and DELETE requests. You can easily implement/customize any HTTP method, though.
11462d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* Supports file upload. Uses memory for small uploads, temp files for large ones.
11562d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* Never caches anything.
11662d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* Does not limit bandwidth, request time or simultaneous connections by default.
11762d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* All header names are converted to lower case so they don't vary between browsers/clients.
11862d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* Persistent connections (Connection "keep-alive") support allowing multiple requests to be served over a single socket connection.
11962d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
12062d6807a93f115642f50732b77b0b03565862e05Jarno Elonen### Websocket
12162d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* Tested on Firefox, Chrome and IE.
12262d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
12362d6807a93f115642f50732b77b0b03565862e05Jarno Elonen### Webserver
12462d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* Default code serves files and shows (prints on console) all HTTP parameters and headers.
12562d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* Supports both dynamic content and file serving.
12662d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* File server supports directory listing, `index.html` and `index.htm`.
12762d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* File server supports partial content (streaming & continue download).
12862d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* File server supports ETags.
12962d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* File server does the 301 redirection trick for directories without `/`.
13062d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* File server serves also very long files without memory overhead.
13162d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* Contains a built-in list of most common MIME types.
13262d6807a93f115642f50732b77b0b03565862e05Jarno Elonen* 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.
133cbdd9d819348d2169d190f305b32d5240d1f60fcMatthieu Brouillard* Simple [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) support via `--cors` paramater
134709bd63d3d1d89e4bb35a2f721d3b2d07cec892dMatthieu Brouillard  * by default serves `Access-Control-Allow-Headers: origin,accept,content-type`
135709bd63d3d1d89e4bb35a2f721d3b2d07cec892dMatthieu Brouillard  * possibility to set `Access-Control-Allow-Headers` by setting System property: `AccessControlAllowHeader`
136709bd63d3d1d89e4bb35a2f721d3b2d07cec892dMatthieu Brouillard  * _example: _ `-DAccessControlAllowHeader=origin,accept,content-type,Authorization`
137cbdd9d819348d2169d190f305b32d5240d1f60fcMatthieu Brouillard  * possible values:
138cbdd9d819348d2169d190f305b32d5240d1f60fcMatthieu Brouillard      * `--cors`: activates CORS support, `Access-Control-Allow-Origin` will be set to `*`
139cbdd9d819348d2169d190f305b32d5240d1f60fcMatthieu Brouillard      * `--cors=some_value`: `Access-Control-Allow-Origin` will be set to `some_value`. 
140cbdd9d819348d2169d190f305b32d5240d1f60fcMatthieu Brouillard
141cbdd9d819348d2169d190f305b32d5240d1f60fcMatthieu Brouillard**_CORS argument examples_**
142cbdd9d819348d2169d190f305b32d5240d1f60fcMatthieu Brouillard
143cbdd9d819348d2169d190f305b32d5240d1f60fcMatthieu Brouillard
144cbdd9d819348d2169d190f305b32d5240d1f60fcMatthieu Brouillard* `--cors=http://appOne.company.com`
145cbdd9d819348d2169d190f305b32d5240d1f60fcMatthieu Brouillard* `--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.
14662d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
14762d6807a93f115642f50732b77b0b03565862e05Jarno Elonen## Maven dependencies
14862d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
14962d6807a93f115642f50732b77b0b03565862e05Jarno ElonenNanoHTTPD is a Maven based project and deployed to central. Most development environments have means to access the central repository. The coordinates to use in Maven are: 
150b3416458940ad54d607b6c4a9c0428de80486752ritchie
151b3416458940ad54d607b6c4a9c0428de80486752ritchie	<dependencies>
152b3416458940ad54d607b6c4a9c0428de80486752ritchie		<dependency>
15334ea556fbb94657ff8be493b937f5e5e75ed0f28ritchie			<groupId>org.nanohttpd</groupId> <!-- <groupId>com.nanohttpd</groupId> for 2.1.0 and earlier -->
154b3416458940ad54d607b6c4a9c0428de80486752ritchie			<artifactId>nanohttpd</artifactId>
155b3416458940ad54d607b6c4a9c0428de80486752ritchie			<version>CURRENT_VERSION</version>
156b3416458940ad54d607b6c4a9c0428de80486752ritchie		</dependency>
157b3416458940ad54d607b6c4a9c0428de80486752ritchie	</dependencies>
158b3416458940ad54d607b6c4a9c0428de80486752ritchie
15962d6807a93f115642f50732b77b0b03565862e05Jarno Elonen(Replace `CURRENT_VERSION` with whatever is reported latest at <http://nanohttpd.org/>.)
16062d6807a93f115642f50732b77b0b03565862e05Jarno Elonen
16162d6807a93f115642f50732b77b0b03565862e05Jarno ElonenThe coordinates for your development environment should correspond to these. When looking for an older version take care because we switched groupId from *com.nanohttpd* to *org.nanohttpd* in mid 2015.
162b3416458940ad54d607b6c4a9c0428de80486752ritchie
16362d6807a93f115642f50732b77b0b03565862e05Jarno ElonenNext it depends what you are useing nanohttpd for, there are tree main usages.
164b3416458940ad54d607b6c4a9c0428de80486752ritchie
1652b3d434d8f056d4284a10b441fe1d6998d96ff3eritchie## Gradle dependencies
1662b3d434d8f056d4284a10b441fe1d6998d96ff3eritchie
1672b3d434d8f056d4284a10b441fe1d6998d96ff3eritchieIn gradle you can use nano http the same way because gradle accesses the same central repository:
1682b3d434d8f056d4284a10b441fe1d6998d96ff3eritchie
1692b3d434d8f056d4284a10b441fe1d6998d96ff3eritchie	dependencies {
1702b3d434d8f056d4284a10b441fe1d6998d96ff3eritchie		runtime(
1712b3d434d8f056d4284a10b441fe1d6998d96ff3eritchie			[group: 'org.nanohttpd', name: 'nanohttpd', version: 'CURRENT_VERSION'],
1722b3d434d8f056d4284a10b441fe1d6998d96ff3eritchie		)
1732b3d434d8f056d4284a10b441fe1d6998d96ff3eritchie	}
1742b3d434d8f056d4284a10b441fe1d6998d96ff3eritchie
1752b3d434d8f056d4284a10b441fe1d6998d96ff3eritchie(Replace `CURRENT_VERSION` with whatever is reported latest at <http://nanohttpd.org/>.)
1762b3d434d8f056d4284a10b441fe1d6998d96ff3eritchie
1772b3d434d8f056d4284a10b441fe1d6998d96ff3eritchieJust replace the name with the artifact id of the module you want to use and gradle will find it for you. 
1782b3d434d8f056d4284a10b441fe1d6998d96ff3eritchie
17962d6807a93f115642f50732b77b0b03565862e05Jarno Elonen### Develop your own specialized HTTP service
180b3416458940ad54d607b6c4a9c0428de80486752ritchie
18162d6807a93f115642f50732b77b0b03565862e05Jarno ElonenFor a specialized HTTP (HTTPS) service you can use the module with artifactId *nanohttpd*.
182b3416458940ad54d607b6c4a9c0428de80486752ritchie
183b3416458940ad54d607b6c4a9c0428de80486752ritchie		<dependency>
18434ea556fbb94657ff8be493b937f5e5e75ed0f28ritchie			<groupId>org.nanohttpd</groupId> <!-- <groupId>com.nanohttpd</groupId> for 2.1.0 and earlier -->
185b3416458940ad54d607b6c4a9c0428de80486752ritchie			<artifactId>nanohttpd</artifactId>
186b3416458940ad54d607b6c4a9c0428de80486752ritchie			<version>CURRENT_VERSION</version>
187b3416458940ad54d607b6c4a9c0428de80486752ritchie		</dependency>
188b3416458940ad54d607b6c4a9c0428de80486752ritchie		
18962d6807a93f115642f50732b77b0b03565862e05Jarno ElonenHere you write your own subclass of *fi.iki.elonen.NanoHTTPD* to configure and to serve the requests.
190b3416458940ad54d607b6c4a9c0428de80486752ritchie  
19162d6807a93f115642f50732b77b0b03565862e05Jarno Elonen### Develop a websocket based service    
192b3416458940ad54d607b6c4a9c0428de80486752ritchie
19362d6807a93f115642f50732b77b0b03565862e05Jarno ElonenFor a specialized websocket service you can use the module with artifactId *nanohttpd-websocket*.
194b3416458940ad54d607b6c4a9c0428de80486752ritchie
195b3416458940ad54d607b6c4a9c0428de80486752ritchie		<dependency>
19634ea556fbb94657ff8be493b937f5e5e75ed0f28ritchie			<groupId>org.nanohttpd</groupId> <!-- <groupId>com.nanohttpd</groupId> for 2.1.0 and earlier -->
197b3416458940ad54d607b6c4a9c0428de80486752ritchie			<artifactId>nanohttpd-websocket</artifactId>
198b3416458940ad54d607b6c4a9c0428de80486752ritchie			<version>CURRENT_VERSION</version>
199b3416458940ad54d607b6c4a9c0428de80486752ritchie		</dependency>
200b3416458940ad54d607b6c4a9c0428de80486752ritchie
20162d6807a93f115642f50732b77b0b03565862e05Jarno ElonenHere you write your own subclass of *fi.iki.elonen.NanoWebSocketServer* to configure and to serve the websocket requests. A small standard echo example is included as *fi.iki.elonen.samples.echo.DebugWebSocketServer*. You can use it as a starting point to implement your own services.
202b3416458940ad54d607b6c4a9c0428de80486752ritchie
20362d6807a93f115642f50732b77b0b03565862e05Jarno Elonen### Develop a custom HTTP file server    
204b3416458940ad54d607b6c4a9c0428de80486752ritchie
20562d6807a93f115642f50732b77b0b03565862e05Jarno ElonenFor a more classic aproach, perhaps to just create a HTTP server serving mostly service files from your disk, you can use the module with artifactId *nanohttpd-webserver*.
206b3416458940ad54d607b6c4a9c0428de80486752ritchie
207b3416458940ad54d607b6c4a9c0428de80486752ritchie		<dependency>
208b3416458940ad54d607b6c4a9c0428de80486752ritchie			<groupId>org.nanohttpd</groupId>
209b3416458940ad54d607b6c4a9c0428de80486752ritchie			<artifactId>nanohttpd-webserver</artifactId>
210b3416458940ad54d607b6c4a9c0428de80486752ritchie			<version>CURRENT_VERSION</version>
211b3416458940ad54d607b6c4a9c0428de80486752ritchie		</dependency>
212b3416458940ad54d607b6c4a9c0428de80486752ritchie
21362d6807a93f115642f50732b77b0b03565862e05Jarno ElonenThe included class *fi.iki.elonen.SimpleWebServer* is intended to be used as a starting point for your own implementation but it also can be used as is. Staring the class as is will start a http server on port 8080 and publishing the current directory.  
2149bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie
21562d6807a93f115642f50732b77b0b03565862e05Jarno Elonen### Living on the edge
2167cb7c97c057d57ce4147b1ae0bec82c092f2851aJarno Elonen
21762d6807a93f115642f50732b77b0b03565862e05Jarno ElonenThe latest Github master version can be fetched through sonatype.org:
2189bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie
2199bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie	<dependencies>
2209bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie		<dependency>
2219bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie			<artifactId>nanohttpd</artifactId>
2229bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie			<groupId>org.nanohttpd</groupId>
223b3416458940ad54d607b6c4a9c0428de80486752ritchie			<version>XXXXX-SNAPSHOT</version>
2249bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie		</dependency>
2259bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie	</dependencies>
2269bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie	...
2279bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie	<repositories>
2289bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie		<repository>
2299bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie			<id>sonatype-snapshots</id>
2309bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie			<url>https://oss.sonatype.org/content/repositories/snapshots</url>
2319bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie			<snapshots>
2329bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie				<enabled>true</enabled>
2339bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie			</snapshots>
2349bf3b6490242dcc53f4d3f5303d3dda29db6df1britchie		</repository>
23562d6807a93f115642f50732b77b0b03565862e05Jarno Elonen	</repositories>
2367cb7c97c057d57ce4147b1ae0bec82c092f2851aJarno Elonen
2373ea79cfc3ef8447430e6fcc9570c1551831ff9c4ritchie### generating an self signed ssl certificate
2387cb7c97c057d57ce4147b1ae0bec82c092f2851aJarno Elonen
2393ea79cfc3ef8447430e6fcc9570c1551831ff9c4ritchieJust a hint how to generate a certificate for localhost.
2403ea79cfc3ef8447430e6fcc9570c1551831ff9c4ritchie
2413ea79cfc3ef8447430e6fcc9570c1551831ff9c4ritchie	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
2423ea79cfc3ef8447430e6fcc9570c1551831ff9c4ritchie
2433ea79cfc3ef8447430e6fcc9570c1551831ff9c4ritchieThis 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 
2443ea79cfc3ef8447430e6fcc9570c1551831ff9c4ritchieyou can use:
2453ea79cfc3ef8447430e6fcc9570c1551831ff9c4ritchie
2463ea79cfc3ef8447430e6fcc9570c1551831ff9c4ritchie	server.makeSecure(NanoHTTPD.makeSSLSocketFactory("/keystore.jks", "password".toCharArray()));
2473ea79cfc3ef8447430e6fcc9570c1551831ff9c4ritchie
2483ea79cfc3ef8447430e6fcc9570c1551831ff9c4ritchieBefore you start the server to make Nanohttpd serve https connections, when you make sure 'keystore.jks' is in your classpath .  
2493ea79cfc3ef8447430e6fcc9570c1551831ff9c4ritchie 
25062d6807a93f115642f50732b77b0b03565862e05Jarno Elonen-----
251be09227d868536848a8d3272ebdffb55d2b764eaPaul Hawke
2523c24d71cb9f63f817c358c4f801bec01f1528ad9Paul Hawke*Thank you to everyone who has reported bugs and suggested fixes.*
253