[Back to main] [Printable version] [Leave a comment]

NanoHTTPD [Leave a comment]

A free, simple, tiny (1 java file!), nicely embeddable HTTP server in Java.

Current version: 1.13 (2010-06-27)

NanoHTTPD

Why another Java HTTPD?

I couldn't find a small enough, embeddable and easily modifiable HTTP server that I could just copy and paste into my other Java projects. Every one of them consisted of dozens of .java files and/or jars, usually with - from my point of view - "overkill features" like servlet support, web administration, configuration files, logging etc.

So I made my own. Take it, hopefully you'll find it useful, too.

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 by yourself if you need them.)
  • Supports parameter parsing of GET and POST methods
  • Parameter names must be unique. (Adding support to multiple instance of a parameter is not difficult, but would make the interface a bit more cumbersome to use.)
  • 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
  • All header names are converted lowercase so they don't vary between browsers/clients

Ways to use

  • Run as a standalone app (serves files from current directory and shows requests)
  • Subclass serve() and embed to your own program (see HelloServer.java for a simple example)
  • Call serveFile() from serve() with your own base directory

Download

Changelog

  • 1.13 (2010-06-27): fixed a wrong case in 'range' header
  • 1.12 (2010-01-07): fixed a null ptr exception
  • 1.11 (2008-04-21): fixed a double URI decoding (caused problems when there was a percent-coded percent)
  • 1.10 (2007-02-09): improved browser compatibility by forcing headers lowercase; fixed a POST method over-read bug
  • 1.05 (2006-03-30): honor Content-Length header; support for clients that leave TCP connection open; better MIME support for symlinked files
  • 1.02 (2005-07-08): fixed a stream read starvation bug
  • 1.01 (2003-04-03): first published version

Thank you to everyone who has reported bugs and suggested fixes.

Mp3HTTPD

As a usage example, if you listen to music over the Internet, try out Mp3HTTPD, a slightly modified version of NanoHTTPD that translates on the fly file references in .M3U playlist files to HTTP-URLs.

License

(The Modified BSD Licence)

Copyright © 2001,2005-2010 Jarno Elonen <elonen@iki.fi>

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

[Read 2 comments]