Node Director
Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
fi.iki.elonen.NanoHTTPD Class Reference

A simple, tiny, nicely embeddable HTTP server in Java NanoHTTPD. More...

Inheritance diagram for fi.iki.elonen.NanoHTTPD:
sfi.director.util.NanoHTTPD sfi.director.application.FrontendHTTPServer

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.

Detailed Description

A simple, tiny, nicely embeddable HTTP server in Java

NanoHTTPD.

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)

Constructor & Destructor Documentation

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().

Member Function Documentation

synchronized void fi.iki.elonen.NanoHTTPD.closeAllConnections ( )

Forcibly closes all connections that are open.

References fi.iki.elonen.NanoHTTPD.stop().

ClientHandler fi.iki.elonen.NanoHTTPD.createClientHandler ( final Socket  finalAccept,
final InputStream  inputStream 
)
protected

create a instance of the client handler, subclasses can return a subclass of the ClientHandler.

Parameters
finalAcceptthe socket the cleint is connected to
inputStreamthe input stream
Returns
the client handler

Referenced by fi.iki.elonen.NanoHTTPD.ServerRunnable.run().

ServerRunnable fi.iki.elonen.NanoHTTPD.createServerRunnable ( final int  timeout)
protected

Instantiate the server runnable, can be overwritten by subclasses to provide a subclass of the ServerRunnable.

Parameters
timeoutthe socet timeout to use.
Returns
the server runnable.

Referenced by fi.iki.elonen.NanoHTTPD.start().

Map<String, List<String> > fi.iki.elonen.NanoHTTPD.decodeParameters ( Map< String, String >  parms)
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.

Parameters
parmsoriginal NanoHTTPD parameters values, as passed to the serve() method.
Returns
a map of String (parameter name) to List<String> (a list of the values supplied).
Map<String, List<String> > fi.iki.elonen.NanoHTTPD.decodeParameters ( String  queryString)
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.

Parameters
queryStringa query string pulled from the URL.
Returns
a map of String (parameter name) to List<String> (a list of the values supplied).

References fi.iki.elonen.NanoHTTPD.decodePercent().

String fi.iki.elonen.NanoHTTPD.decodePercent ( String  str)
protected

Decode percent encoded String values.

Parameters
strthe percent encoded String
Returns
expanded form of the input, for example "foo%20bar" becomes "foo bar"

Referenced by fi.iki.elonen.NanoHTTPD.decodeParameters().

final int fi.iki.elonen.NanoHTTPD.getListeningPort ( )
final boolean fi.iki.elonen.NanoHTTPD.isAlive ( )
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 SSLServerSocketFactory fi.iki.elonen.NanoHTTPD.makeSSLSocketFactory ( KeyStore  loadedKeyStore,
KeyManager[]  keyManagers 
) throws IOException
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 SSLServerSocketFactory fi.iki.elonen.NanoHTTPD.makeSSLSocketFactory ( KeyStore  loadedKeyStore,
KeyManagerFactory  loadedKeyFactory 
) throws IOException
static

Creates an SSLSocketFactory for HTTPS.

Pass a loaded KeyStore and a loaded KeyManagerFactory. These objects must properly loaded/initialized by the caller.

static SSLServerSocketFactory fi.iki.elonen.NanoHTTPD.makeSSLSocketFactory ( String  keyAndTrustStoreClasspathPath,
char[]  passphrase 
) throws IOException
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 
)
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)
Response fi.iki.elonen.NanoHTTPD.serve ( IHTTPSession  session)
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.)

Parameters
uriPercent-decoded URI without parameters, for example "/index.cgi"
method"GET", "POST" etc.
parmsParsed, percent decoded parameters from URI and, in case of POST, data.
headersHeader entries, percent decoded
Returns
HTTP response, see class Response for details

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.

Parameters
asyncRunnernew 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.

Parameters
tempFileManagerFactorynew strategy for handling temp files.

Referenced by fi.iki.elonen.NanoHTTPD.NanoHTTPD().

void fi.iki.elonen.NanoHTTPD.start ( ) throws IOException
void fi.iki.elonen.NanoHTTPD.start ( final int  timeout) throws IOException

Start the server.

Parameters
timeouttimeout to use for socket connections.
Exceptions
IOExceptionif the socket is in use.

References fi.iki.elonen.NanoHTTPD.createServerRunnable().

void fi.iki.elonen.NanoHTTPD.stop ( )
boolean fi.iki.elonen.NanoHTTPD.useGzipWhenAccepted ( )
protected
Returns
true if the gzip compression should be used if the client accespts it.

Reimplemented in sfi.director.application.FrontendHTTPServer.

Referenced by fi.iki.elonen.NanoHTTPD.HTTPSession.execute().

final boolean fi.iki.elonen.NanoHTTPD.wasStarted ( )

Member Data Documentation

AsyncRunner fi.iki.elonen.NanoHTTPD.asyncRunner
protected
final String fi.iki.elonen.NanoHTTPD.MIME_HTML = "text/html"
static

Common MIME type for dynamic content: html.

Referenced by fi.iki.elonen.NanoHTTPD.newFixedLengthResponse().

final String fi.iki.elonen.NanoHTTPD.MIME_PLAINTEXT = "text/plain"
static

Common MIME type for dynamic content: plain text.

Referenced by fi.iki.elonen.NanoHTTPD.HTTPSession.execute(), and fi.iki.elonen.NanoHTTPD.serve().

final int fi.iki.elonen.NanoHTTPD.SOCKET_READ_TIMEOUT = 5000
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().


The documentation for this class was generated from the following file: