public abstract class NanoHTTPD
extends java.lang.Object
Copyright (c) 2012-2013 by Paul S. Hawke, 2001,2005-2013 by Jarno Elonen, 2010 by Konstantinos Togias
Features + limitations:Modifier and Type | Class and Description |
---|---|
static interface |
NanoHTTPD.AsyncRunner
Pluggable strategy for asynchronously executing requests.
|
class |
NanoHTTPD.ClientHandler
The runnable that will be used for every new client connection.
|
protected static class |
NanoHTTPD.ContentType |
static class |
NanoHTTPD.Cookie |
class |
NanoHTTPD.CookieHandler
Provides rudimentary support for cookies.
|
static class |
NanoHTTPD.DefaultAsyncRunner
Default threading strategy for NanoHTTPD.
|
static class |
NanoHTTPD.DefaultServerSocketFactory
Creates a normal ServerSocket for TCP connections
|
static class |
NanoHTTPD.DefaultTempFile
Default strategy for creating and cleaning up temporary files.
|
static class |
NanoHTTPD.DefaultTempFileManager
Default strategy for creating and cleaning up temporary files.
|
protected class |
NanoHTTPD.HTTPSession |
static interface |
NanoHTTPD.IHTTPSession
Handles one session, i.e.
|
static class |
NanoHTTPD.Method
HTTP Request methods, with the ability to decode a
String
back to its enum value. |
static class |
NanoHTTPD.Response
HTTP response.
|
static class |
NanoHTTPD.ResponseException |
static class |
NanoHTTPD.SecureServerSocketFactory
Creates a new SSLServerSocket
|
class |
NanoHTTPD.ServerRunnable
The runnable that will be used for the main listening thread.
|
static interface |
NanoHTTPD.ServerSocketFactory
Factory to create ServerSocketFactories.
|
static interface |
NanoHTTPD.TempFile
A temp file.
|
static interface |
NanoHTTPD.TempFileManager
Temp file manager.
|
static interface |
NanoHTTPD.TempFileManagerFactory
Factory to create temp file managers.
|
Modifier and Type | Field and Description |
---|---|
protected NanoHTTPD.AsyncRunner |
asyncRunner
Pluggable strategy for asynchronously executing requests.
|
static java.lang.String |
MIME_HTML
Common MIME type for dynamic content: html
|
static java.lang.String |
MIME_PLAINTEXT
Common MIME type for dynamic content: plain text
|
protected static java.util.Map<java.lang.String,java.lang.String> |
MIME_TYPES
Hashtable mapping (String)FILENAME_EXTENSION -> (String)MIME_TYPE
|
static int |
SOCKET_READ_TIMEOUT
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).
|
Constructor and Description |
---|
NanoHTTPD(int port)
Constructs an HTTP server on given port.
|
NanoHTTPD(java.lang.String hostname,
int port)
Constructs an HTTP server on given hostname and port.
|
Modifier and Type | Method and Description |
---|---|
void |
closeAllConnections()
Forcibly closes all connections that are open.
|
protected NanoHTTPD.ClientHandler |
createClientHandler(java.net.Socket finalAccept,
java.io.InputStream inputStream)
create a instance of the client handler, subclasses can return a subclass
of the ClientHandler.
|
protected NanoHTTPD.ServerRunnable |
createServerRunnable(int timeout)
Instantiate the server runnable, can be overwritten by subclasses to
provide a subclass of the ServerRunnable.
|
protected static java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
decodeParameters(java.util.Map<java.lang.String,java.lang.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.
|
protected static java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
decodeParameters(java.lang.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.
|
protected static java.lang.String |
decodePercent(java.lang.String str)
Decode percent encoded
String values. |
java.lang.String |
getHostname() |
int |
getListeningPort() |
static java.lang.String |
getMimeTypeForFile(java.lang.String uri)
Get MIME type from file name extension, if possible
|
NanoHTTPD.ServerSocketFactory |
getServerSocketFactory() |
NanoHTTPD.TempFileManagerFactory |
getTempFileManagerFactory() |
boolean |
isAlive() |
void |
makeSecure(javax.net.ssl.SSLServerSocketFactory sslServerSocketFactory,
java.lang.String[] sslProtocols)
Call before start() to serve over HTTPS instead of HTTP
|
static javax.net.ssl.SSLServerSocketFactory |
makeSSLSocketFactory(java.security.KeyStore loadedKeyStore,
javax.net.ssl.KeyManager[] keyManagers)
Creates an SSLSocketFactory for HTTPS.
|
static javax.net.ssl.SSLServerSocketFactory |
makeSSLSocketFactory(java.security.KeyStore loadedKeyStore,
javax.net.ssl.KeyManagerFactory loadedKeyFactory)
Creates an SSLSocketFactory for HTTPS.
|
static javax.net.ssl.SSLServerSocketFactory |
makeSSLSocketFactory(java.lang.String keyAndTrustStoreClasspathPath,
char[] passphrase)
Creates an SSLSocketFactory for HTTPS.
|
static java.util.Map<java.lang.String,java.lang.String> |
mimeTypes() |
static NanoHTTPD.Response |
newChunkedResponse(NanoHTTPD.Response.IStatus status,
java.lang.String mimeType,
java.io.InputStream data)
Create a response with unknown length (using HTTP 1.1 chunking).
|
static NanoHTTPD.Response |
newFixedLengthResponse(NanoHTTPD.Response.IStatus status,
java.lang.String mimeType,
java.io.InputStream data,
long totalBytes)
Create a response with known length.
|
static NanoHTTPD.Response |
newFixedLengthResponse(NanoHTTPD.Response.IStatus status,
java.lang.String mimeType,
java.lang.String txt)
Create a text response with known length.
|
static NanoHTTPD.Response |
newFixedLengthResponse(java.lang.String msg)
Create a text response with known length.
|
NanoHTTPD.Response |
serve(NanoHTTPD.IHTTPSession session)
Override this to customize the server.
|
NanoHTTPD.Response |
serve(java.lang.String uri,
NanoHTTPD.Method method,
java.util.Map<java.lang.String,java.lang.String> headers,
java.util.Map<java.lang.String,java.lang.String> parms,
java.util.Map<java.lang.String,java.lang.String> files)
Deprecated.
|
void |
setAsyncRunner(NanoHTTPD.AsyncRunner asyncRunner)
Pluggable strategy for asynchronously executing requests.
|
void |
setServerSocketFactory(NanoHTTPD.ServerSocketFactory serverSocketFactory) |
void |
setTempFileManagerFactory(NanoHTTPD.TempFileManagerFactory tempFileManagerFactory)
Pluggable strategy for creating and cleaning up temporary files.
|
void |
start()
Start the server.
|
void |
start(int timeout)
Starts the server (in setDaemon(true) mode).
|
void |
start(int timeout,
boolean daemon)
Start the server.
|
void |
stop()
Stop the server.
|
protected boolean |
useGzipWhenAccepted(NanoHTTPD.Response r) |
boolean |
wasStarted() |
public static final int SOCKET_READ_TIMEOUT
public static final java.lang.String MIME_PLAINTEXT
public static final java.lang.String MIME_HTML
protected static java.util.Map<java.lang.String,java.lang.String> MIME_TYPES
protected NanoHTTPD.AsyncRunner asyncRunner
public NanoHTTPD(int port)
public NanoHTTPD(java.lang.String hostname, int port)
public static java.util.Map<java.lang.String,java.lang.String> mimeTypes()
public static javax.net.ssl.SSLServerSocketFactory makeSSLSocketFactory(java.security.KeyStore loadedKeyStore, javax.net.ssl.KeyManager[] keyManagers) throws java.io.IOException
java.io.IOException
public static javax.net.ssl.SSLServerSocketFactory makeSSLSocketFactory(java.security.KeyStore loadedKeyStore, javax.net.ssl.KeyManagerFactory loadedKeyFactory) throws java.io.IOException
java.io.IOException
public static javax.net.ssl.SSLServerSocketFactory makeSSLSocketFactory(java.lang.String keyAndTrustStoreClasspathPath, char[] passphrase) throws java.io.IOException
java.io.IOException
public static java.lang.String getMimeTypeForFile(java.lang.String uri)
uri
- the string representing a filepublic void closeAllConnections()
protected NanoHTTPD.ClientHandler createClientHandler(java.net.Socket finalAccept, java.io.InputStream inputStream)
finalAccept
- the socket the cleint is connected toinputStream
- the input streamprotected NanoHTTPD.ServerRunnable createServerRunnable(int timeout)
timeout
- the socet timeout to use.protected static java.util.Map<java.lang.String,java.util.List<java.lang.String>> decodeParameters(java.util.Map<java.lang.String,java.lang.String> parms)
parms
- original NanoHTTPD parameters values, as passed to the
serve()
method.String
(parameter name) to
List<String>
(a list of the values supplied).protected static java.util.Map<java.lang.String,java.util.List<java.lang.String>> decodeParameters(java.lang.String queryString)
queryString
- a query string pulled from the URL.String
(parameter name) to
List<String>
(a list of the values supplied).protected static java.lang.String decodePercent(java.lang.String str)
String
values.str
- the percent encoded String
protected boolean useGzipWhenAccepted(NanoHTTPD.Response r)
public final int getListeningPort()
public final boolean isAlive()
public NanoHTTPD.ServerSocketFactory getServerSocketFactory()
public void setServerSocketFactory(NanoHTTPD.ServerSocketFactory serverSocketFactory)
public java.lang.String getHostname()
public NanoHTTPD.TempFileManagerFactory getTempFileManagerFactory()
public void makeSecure(javax.net.ssl.SSLServerSocketFactory sslServerSocketFactory, java.lang.String[] sslProtocols)
public static NanoHTTPD.Response newChunkedResponse(NanoHTTPD.Response.IStatus status, java.lang.String mimeType, java.io.InputStream data)
public static NanoHTTPD.Response newFixedLengthResponse(NanoHTTPD.Response.IStatus status, java.lang.String mimeType, java.io.InputStream data, long totalBytes)
public static NanoHTTPD.Response newFixedLengthResponse(NanoHTTPD.Response.IStatus status, java.lang.String mimeType, java.lang.String txt)
public static NanoHTTPD.Response newFixedLengthResponse(java.lang.String msg)
public NanoHTTPD.Response serve(NanoHTTPD.IHTTPSession session)
session
- The HTTP session@Deprecated public NanoHTTPD.Response serve(java.lang.String uri, NanoHTTPD.Method method, java.util.Map<java.lang.String,java.lang.String> headers, java.util.Map<java.lang.String,java.lang.String> parms, java.util.Map<java.lang.String,java.lang.String> files)
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 decodedpublic void setAsyncRunner(NanoHTTPD.AsyncRunner asyncRunner)
asyncRunner
- new strategy for handling threads.public void setTempFileManagerFactory(NanoHTTPD.TempFileManagerFactory tempFileManagerFactory)
tempFileManagerFactory
- new strategy for handling temp files.public void start() throws java.io.IOException
java.io.IOException
- if the socket is in use.public void start(int timeout) throws java.io.IOException
java.io.IOException
public void start(int timeout, boolean daemon) throws java.io.IOException
timeout
- timeout to use for socket connections.daemon
- start the thread daemon or not.java.io.IOException
- if the socket is in use.public void stop()
public final boolean wasStarted()