Node Director
|
A simple, tiny, nicely embeddable HTTP server in Java NanoHTTPD. More...
Classes | |
interface | AsyncRunner |
Pluggable strategy for asynchronously executing requests. More... | |
class | ClientHandler |
The runnable that will be used for every new client connection. More... | |
class | Cookie |
class | CookieHandler |
Provides rudimentary support for cookies. More... | |
class | DefaultAsyncRunner |
Default threading strategy for NanoHTTPD. | |
class | DefaultTempFile |
Default strategy for creating and cleaning up temporary files. | |
class | DefaultTempFileManager |
Default strategy for creating and cleaning up temporary files. | |
class | DefaultTempFileManagerFactory |
Default strategy for creating and cleaning up temporary files. | |
class | HTTPSession |
interface | IHTTPSession |
Handles one session, i.e. More... | |
enum | Method |
HTTP Request methods, with the ability to decode a String back to its enum value. More... | |
class | Response |
HTTP response. | |
class | ResponseException |
class | ServerRunnable |
The runnable that will be used for the main listening thread. More... | |
interface | TempFile |
A temp file. More... | |
interface | TempFileManager |
Temp file manager. More... | |
interface | TempFileManagerFactory |
Factory to create temp file managers. More... |
Public Member Functions | |
NanoHTTPD (int port) | |
Constructs an HTTP server on given port. | |
NanoHTTPD (String hostname, int port) | |
Constructs an HTTP server on given hostname and port. | |
synchronized void | closeAllConnections () |
Forcibly closes all connections that are open. | |
final int | getListeningPort () |
final boolean | isAlive () |
void | makeSecure (SSLServerSocketFactory sslServerSocketFactory) |
Call before start() to serve over HTTPS instead of HTTP. | |
Response | newChunkedResponse (IStatus status, String mimeType, InputStream data) |
Create a response with unknown length (using HTTP 1.1 chunking). | |
Response | newFixedLengthResponse (IStatus status, String mimeType, InputStream data, long totalBytes) |
Create a response with known length. | |
Response | newFixedLengthResponse (IStatus status, String mimeType, String txt) |
Create a text response with known length. | |
Response | newFixedLengthResponse (String msg) |
Create a text response with known length. | |
Response | serve (IHTTPSession session) |
Override this to customize the server. | |
Response | serve (String uri, Method method, Map< String, String > headers, Map< String, String > parms, Map< String, String > files) |
Override this to customize the server. | |
void | setAsyncRunner (AsyncRunner asyncRunner) |
Pluggable strategy for asynchronously executing requests. | |
void | setTempFileManagerFactory (TempFileManagerFactory tempFileManagerFactory) |
Pluggable strategy for creating and cleaning up temporary files. | |
void | start () throws IOException |
Start the server. | |
void | start (final int timeout) throws IOException |
Start the server. | |
void | stop () |
Stop the server. | |
final boolean | wasStarted () |
Static Public Member Functions | |
static SSLServerSocketFactory | makeSSLSocketFactory (KeyStore loadedKeyStore, KeyManager[] keyManagers) throws IOException |
Creates an SSLSocketFactory for HTTPS. | |
static SSLServerSocketFactory | makeSSLSocketFactory (KeyStore loadedKeyStore, KeyManagerFactory loadedKeyFactory) throws IOException |
Creates an SSLSocketFactory for HTTPS. | |
static SSLServerSocketFactory | makeSSLSocketFactory (String keyAndTrustStoreClasspathPath, char[] passphrase) throws IOException |
Creates an SSLSocketFactory for HTTPS. |
Static Public Attributes | |
static final int | SOCKET_READ_TIMEOUT = 5000 |
Maximum time to wait on Socket.getInputStream().read() (in milliseconds) This is required as the Keep-Alive HTTP connections would otherwise block the socket reading thread forever (or as long the browser is open). | |
static final String | MIME_PLAINTEXT = "text/plain" |
Common MIME type for dynamic content: plain text. | |
static final String | MIME_HTML = "text/html" |
Common MIME type for dynamic content: html. |
Protected Member Functions | |
ClientHandler | createClientHandler (final Socket finalAccept, final InputStream inputStream) |
create a instance of the client handler, subclasses can return a subclass of the ClientHandler. | |
ServerRunnable | createServerRunnable (final int timeout) |
Instantiate the server runnable, can be overwritten by subclasses to provide a subclass of the ServerRunnable. | |
Map< String, List< String > > | decodeParameters (Map< String, String > parms) |
Decode parameters from a URL, handing the case where a single parameter name might have been supplied several times, by return lists of values. | |
Map< String, List< String > > | decodeParameters (String queryString) |
Decode parameters from a URL, handing the case where a single parameter name might have been supplied several times, by return lists of values. | |
String | decodePercent (String str) |
Decode percent encoded String values. | |
boolean | useGzipWhenAccepted () |
Protected Attributes | |
AsyncRunner | asyncRunner |
Pluggable strategy for asynchronously executing requests. |
A simple, tiny, nicely embeddable HTTP server in Java
Copyright (c) 2012-2013 by Paul S. Hawke, 2001,2005-2013 by Jarno Elonen, 2010 by Konstantinos Togias
Features + limitations:
Only one Java file Java 5 compatible Released as open source, Modified BSD licence No fixed config files, logging, authorization etc. (Implement yourself if you need them.) Supports parameter parsing of GET and POST methods (+ rudimentary PUT support in 1.25) Supports both dynamic content and file serving Supports file upload (since version 1.2, 2010) Supports partial content (streaming) Supports ETags Never caches anything Doesn't limit bandwidth, request time or simultaneous connections Default code serves files and shows all HTTP parameters and headers File server supports directory listing, index.html and index.htm File server supports partial content (streaming) File server supports ETags File server does the 301 redirection trick for directories without '/' File server supports simple skipping for files (continue download) File server serves also very long files without memory overhead Contains a built-in list of most common MIME types All header names are converted to lower case so they don't vary between browsers/clients
How to use:
Subclass and implement serve() and embed to your own program
See the separate "LICENSE.md" file for the distribution license (Modified BSD licence)
fi.iki.elonen.NanoHTTPD.NanoHTTPD | ( | int | port | ) |
Constructs an HTTP server on given port.
Reimplemented in sfi.director.util.NanoHTTPD.
fi.iki.elonen.NanoHTTPD.NanoHTTPD | ( | String | hostname, |
int | port | ||
) |
Constructs an HTTP server on given hostname and port.
References fi.iki.elonen.NanoHTTPD.setAsyncRunner(), and fi.iki.elonen.NanoHTTPD.setTempFileManagerFactory().
synchronized void fi.iki.elonen.NanoHTTPD.closeAllConnections | ( | ) |
Forcibly closes all connections that are open.
References fi.iki.elonen.NanoHTTPD.stop().
|
protected |
create a instance of the client handler, subclasses can return a subclass of the ClientHandler.
finalAccept | the socket the cleint is connected to |
inputStream | the input stream |
Referenced by fi.iki.elonen.NanoHTTPD.ServerRunnable.run().
|
protected |
Instantiate the server runnable, can be overwritten by subclasses to provide a subclass of the ServerRunnable.
timeout | the socet timeout to use. |
Referenced by fi.iki.elonen.NanoHTTPD.start().
|
protected |
Decode parameters from a URL, handing the case where a single parameter name might have been supplied several times, by return lists of values.
In general these lists will contain a single element.
String
(parameter name) to List<String>
(a list of the values supplied).
|
protected |
Decode parameters from a URL, handing the case where a single parameter name might have been supplied several times, by return lists of values.
In general these lists will contain a single element.
queryString | a query string pulled from the URL. |
String
(parameter name) to List<String>
(a list of the values supplied). References fi.iki.elonen.NanoHTTPD.decodePercent().
|
protected |
Decode percent encoded String
values.
str | the percent encoded String |
Referenced by fi.iki.elonen.NanoHTTPD.decodeParameters().
final int fi.iki.elonen.NanoHTTPD.getListeningPort | ( | ) |
Referenced by sfi.director.cli.frinterface.run().
final boolean fi.iki.elonen.NanoHTTPD.isAlive | ( | ) |
References fi.iki.elonen.NanoHTTPD.wasStarted().
void fi.iki.elonen.NanoHTTPD.makeSecure | ( | SSLServerSocketFactory | sslServerSocketFactory | ) |
Call before start() to serve over HTTPS instead of HTTP.
Referenced by sfi.director.application.FrontendHTTPServer.FrontendHTTPServer().
|
static |
Creates an SSLSocketFactory for HTTPS.
Pass a loaded KeyStore and an array of loaded KeyManagers. These objects must properly loaded/initialized by the caller.
|
static |
Creates an SSLSocketFactory for HTTPS.
Pass a loaded KeyStore and a loaded KeyManagerFactory. These objects must properly loaded/initialized by the caller.
|
static |
Creates an SSLSocketFactory for HTTPS.
Pass a KeyStore resource with your certificate and passphrase
Response fi.iki.elonen.NanoHTTPD.newChunkedResponse | ( | IStatus | status, |
String | mimeType, | ||
InputStream | data | ||
) |
Create a response with unknown length (using HTTP 1.1 chunking).
Referenced by sfi.director.application.FrontendHTTPServer.sessionProxy().
Response fi.iki.elonen.NanoHTTPD.newFixedLengthResponse | ( | IStatus | status, |
String | mimeType, | ||
InputStream | data, | ||
long | totalBytes | ||
) |
Create a response with known length.
Referenced by fi.iki.elonen.NanoHTTPD.HTTPSession.execute(), sfi.director.application.FrontendHTTPServer.handleRpc(), fi.iki.elonen.NanoHTTPD.newFixedLengthResponse(), sfi.director.application.FrontendHTTPServer.redirect(), sfi.director.application.FrontendHTTPServer.serve(), fi.iki.elonen.NanoHTTPD.serve(), and sfi.director.application.FrontendHTTPServer.sessionProxy().
Response fi.iki.elonen.NanoHTTPD.newFixedLengthResponse | ( | IStatus | status, |
String | mimeType, | ||
String | txt | ||
) |
Create a text response with known length.
References fi.iki.elonen.NanoHTTPD.newFixedLengthResponse().
Response fi.iki.elonen.NanoHTTPD.newFixedLengthResponse | ( | String | msg | ) |
Create a text response with known length.
References fi.iki.elonen.NanoHTTPD.MIME_HTML, fi.iki.elonen.NanoHTTPD.newFixedLengthResponse(), and fi.iki.elonen.NanoHTTPD.Response.Status.OK.
Response fi.iki.elonen.NanoHTTPD.serve | ( | IHTTPSession | session | ) |
Override this to customize the server.
(By default, this returns a 404 "Not Found" plain text error response.)
session | The HTTP session |
References fi.iki.elonen.NanoHTTPD.IHTTPSession.getHeaders(), fi.iki.elonen.NanoHTTPD.IHTTPSession.getMethod(), fi.iki.elonen.NanoHTTPD.IHTTPSession.getParms(), fi.iki.elonen.NanoHTTPD.IHTTPSession.getQueryParameterString(), fi.iki.elonen.NanoHTTPD.IHTTPSession.getUri(), fi.iki.elonen.NanoHTTPD.MIME_PLAINTEXT, fi.iki.elonen.NanoHTTPD.newFixedLengthResponse(), fi.iki.elonen.NanoHTTPD.IHTTPSession.parseBody(), fi.iki.elonen.NanoHTTPD.Method.POST, and fi.iki.elonen.NanoHTTPD.Method.PUT.
Referenced by fi.iki.elonen.NanoHTTPD.HTTPSession.execute().
Response fi.iki.elonen.NanoHTTPD.serve | ( | String | uri, |
Method | method, | ||
Map< String, String > | headers, | ||
Map< String, String > | parms, | ||
Map< String, String > | files | ||
) |
Override this to customize the server.
(By default, this returns a 404 "Not Found" plain text error response.)
uri | Percent-decoded URI without parameters, for example "/index.cgi" |
method | "GET", "POST" etc. |
parms | Parsed, percent decoded parameters from URI and, in case of POST, data. |
headers | Header entries, percent decoded |
References fi.iki.elonen.NanoHTTPD.MIME_PLAINTEXT, and fi.iki.elonen.NanoHTTPD.newFixedLengthResponse().
void fi.iki.elonen.NanoHTTPD.setAsyncRunner | ( | AsyncRunner | asyncRunner | ) |
Pluggable strategy for asynchronously executing requests.
asyncRunner | new strategy for handling threads. |
References fi.iki.elonen.NanoHTTPD.asyncRunner.
Referenced by fi.iki.elonen.NanoHTTPD.NanoHTTPD().
void fi.iki.elonen.NanoHTTPD.setTempFileManagerFactory | ( | TempFileManagerFactory | tempFileManagerFactory | ) |
Pluggable strategy for creating and cleaning up temporary files.
tempFileManagerFactory | new strategy for handling temp files. |
Referenced by fi.iki.elonen.NanoHTTPD.NanoHTTPD().
void fi.iki.elonen.NanoHTTPD.start | ( | ) | throws IOException |
Start the server.
IOException | if the socket is in use. |
References fi.iki.elonen.NanoHTTPD.SOCKET_READ_TIMEOUT.
Referenced by sfi.director.application.FrontendHTTPServer.db_searchget(), and sfi.director.application.FrontendHTTPServer.FrontendHTTPServer().
void fi.iki.elonen.NanoHTTPD.start | ( | final int | timeout | ) | throws IOException |
Start the server.
timeout | timeout to use for socket connections. |
IOException | if the socket is in use. |
References fi.iki.elonen.NanoHTTPD.createServerRunnable().
void fi.iki.elonen.NanoHTTPD.stop | ( | ) |
Stop the server.
References fi.iki.elonen.NanoHTTPD.asyncRunner, and fi.iki.elonen.NanoHTTPD.AsyncRunner.closeAll().
Referenced by fi.iki.elonen.NanoHTTPD.closeAllConnections().
|
protected |
Reimplemented in sfi.director.application.FrontendHTTPServer.
Referenced by fi.iki.elonen.NanoHTTPD.HTTPSession.execute().
final boolean fi.iki.elonen.NanoHTTPD.wasStarted | ( | ) |
Referenced by fi.iki.elonen.NanoHTTPD.isAlive().
|
protected |
Pluggable strategy for asynchronously executing requests.
Referenced by fi.iki.elonen.NanoHTTPD.ClientHandler.run(), fi.iki.elonen.NanoHTTPD.ServerRunnable.run(), fi.iki.elonen.NanoHTTPD.setAsyncRunner(), and fi.iki.elonen.NanoHTTPD.stop().
|
static |
Common MIME type for dynamic content: html.
Referenced by fi.iki.elonen.NanoHTTPD.newFixedLengthResponse().
|
static |
Common MIME type for dynamic content: plain text.
Referenced by fi.iki.elonen.NanoHTTPD.HTTPSession.execute(), and fi.iki.elonen.NanoHTTPD.serve().
|
static |
Maximum time to wait on Socket.getInputStream().read() (in milliseconds) This is required as the Keep-Alive HTTP connections would otherwise block the socket reading thread forever (or as long the browser is open).
Referenced by fi.iki.elonen.NanoHTTPD.start().