Package contrib

Class NanoHTTPD

  • Direct Known Subclasses:
    Moconti

    public class NanoHTTPD
    extends java.lang.Object
    A simple, tiny, nicely embeddable HTTP 1.0 server in Java

    NanoHTTPD version 1.05, Copyright © 2001,2005,2006 Jarno Elonen (elonen@iki.fi, http://iki.fi/elonen/)

    Features & limitations:

    • Only one Java file
    • Java 1.1 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
    • Supports both dynamic content and file serving
    • 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 does the 301 redirection trick for directories without '/'
    • File server supports simple skipping for files (continue download)
    • File server uses current directory as a web root
    • File server serves also very long files without memory overhead
    • Contains a built-in list of most common mime types

    Raffi-Modifications:

    • Added a check to prevent serving hidden files
    • Added an option -Dbind.interface=127.0.0.1 to bind Moconti to a specific interface
    • HTTP Cache Support for default serve() implementation
    • Loads additional mime types from a mime.types file located in the current working directory.
    • Added suport for %uxxxx percent encoding. Not standard but try telling that to users
    • Fixed POST data corruption bug that surfaced with large post sets in Java 1.6.

    Ways to use:

    • Run as a standalone app, serves files from current directory and shows requests
    • Subclass serve() and embed to your own program
    • Call serveFile() from serve() with your own base directory
    See the end of the source file for distribution license (Modified BSD licence)
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      class  NanoHTTPD.Response
      HTTP response.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String HTTP_BADREQUEST
      Some HTTP response status codes
      static java.lang.String HTTP_FORBIDDEN
      Some HTTP response status codes
      static java.lang.String HTTP_INTERNALERROR
      Some HTTP response status codes
      static java.lang.String HTTP_NOT_MODIFIED
      Some HTTP response status codes
      static java.lang.String HTTP_NOTFOUND
      Some HTTP response status codes
      static java.lang.String HTTP_NOTIMPLEMENTED
      Some HTTP response status codes
      static java.lang.String HTTP_OK
      Some HTTP response status codes
      static java.lang.String HTTP_REDIRECT
      Some HTTP response status codes
      static java.lang.String MIME_DEFAULT_BINARY
      Common mime types for dynamic content
      static java.lang.String MIME_HTML
      Common mime types for dynamic content
      static java.lang.String MIME_PLAINTEXT
      Common mime types for dynamic content
    • Constructor Summary

      Constructors 
      Constructor Description
      NanoHTTPD​(int port)
      Inits an HTTP server to given port.
      NanoHTTPD​(int port, boolean listen)
      Inits an HTTP server to given port.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void listen()
      Starts the HTTP server listening on the port it was initialized with
      static void main​(java.lang.String[] args)
      Starts as a standalone file server and waits for Enter.
      NanoHTTPD.Response serve​(java.lang.String uri, java.lang.String method, java.util.Properties header, java.util.Properties parms)
      Override this to customize the server.
      NanoHTTPD.Response serveFile​(java.lang.String uri, java.util.Properties header, java.io.File homeDir, boolean allowDirectoryListing)
      Serves file from homeDir and its' subdirectories (only).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • HTTP_OK

        public static final java.lang.String HTTP_OK
        Some HTTP response status codes
        See Also:
        Constant Field Values
      • HTTP_REDIRECT

        public static final java.lang.String HTTP_REDIRECT
        Some HTTP response status codes
        See Also:
        Constant Field Values
      • HTTP_NOT_MODIFIED

        public static final java.lang.String HTTP_NOT_MODIFIED
        Some HTTP response status codes
        See Also:
        Constant Field Values
      • HTTP_FORBIDDEN

        public static final java.lang.String HTTP_FORBIDDEN
        Some HTTP response status codes
        See Also:
        Constant Field Values
      • HTTP_NOTFOUND

        public static final java.lang.String HTTP_NOTFOUND
        Some HTTP response status codes
        See Also:
        Constant Field Values
      • HTTP_BADREQUEST

        public static final java.lang.String HTTP_BADREQUEST
        Some HTTP response status codes
        See Also:
        Constant Field Values
      • HTTP_INTERNALERROR

        public static final java.lang.String HTTP_INTERNALERROR
        Some HTTP response status codes
        See Also:
        Constant Field Values
      • HTTP_NOTIMPLEMENTED

        public static final java.lang.String HTTP_NOTIMPLEMENTED
        Some HTTP response status codes
        See Also:
        Constant Field Values
      • MIME_PLAINTEXT

        public static final java.lang.String MIME_PLAINTEXT
        Common mime types for dynamic content
        See Also:
        Constant Field Values
      • MIME_HTML

        public static final java.lang.String MIME_HTML
        Common mime types for dynamic content
        See Also:
        Constant Field Values
      • MIME_DEFAULT_BINARY

        public static final java.lang.String MIME_DEFAULT_BINARY
        Common mime types for dynamic content
        See Also:
        Constant Field Values
    • Constructor Detail

      • NanoHTTPD

        public NanoHTTPD​(int port)
                  throws java.io.IOException
        Inits an HTTP server to given port.

        Throws an IOException if the socket is already in use

        Throws:
        java.io.IOException
      • NanoHTTPD

        public NanoHTTPD​(int port,
                         boolean listen)
                  throws java.io.IOException
        Inits an HTTP server to given port. Starts listening if value is set to true. Throws an IOException if the socket is already in use
        Throws:
        java.io.IOException
    • Method Detail

      • serve

        public NanoHTTPD.Response serve​(java.lang.String uri,
                                        java.lang.String method,
                                        java.util.Properties header,
                                        java.util.Properties parms)
        Override this to customize the server.

        (By default, this delegates to serveFile() and allows directory listing.)

        Returns:
        HTTP response, see class Response for details
      • listen

        public void listen()
                    throws java.io.IOException
        Starts the HTTP server listening on the port it was initialized with
        Throws:
        java.io.IOException
      • main

        public static void main​(java.lang.String[] args)
        Starts as a standalone file server and waits for Enter.
      • serveFile

        public NanoHTTPD.Response serveFile​(java.lang.String uri,
                                            java.util.Properties header,
                                            java.io.File homeDir,
                                            boolean allowDirectoryListing)
        Serves file from homeDir and its' subdirectories (only). Uses only URI, ignores all headers and HTTP parameters.