Class HttpServer

  • Direct Known Subclasses:
    CorsHttpServer

    public class HttpServer
    extends java.lang.Object
    Simple modular HTTP server. One thread is started per request. Connections are not kept open between requests. Suitable for very large response bodies, but not for very large request bodies. Add one or more HttpServer.Handlers to serve actual requests. The protocol version served is HTTP/1.0.

    This class is completely self-contained, so that it can easily be lifted out and used in other packages if required.

    Since:
    21 Aug 2008
    Author:
    Mark Taylor
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  HttpServer.Handler
      Implemented to serve data for some URLs.
      static class  HttpServer.Request
      Represents a parsed HTTP client request.
      static class  HttpServer.Response
      Represents a response to an HTTP request.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String HDR_CONTENT_TYPE
      Header string for MIME content type.
      static int STATUS_OK
      Status code for OK (200).
    • Constructor Summary

      Constructors 
      Constructor Description
      HttpServer()
      Constructs a server based on a default socket, on any free port.
      HttpServer​(java.net.ServerSocket socket)
      Constructs a server based on a given socket.
    • Field Detail

      • HDR_CONTENT_TYPE

        public static final java.lang.String HDR_CONTENT_TYPE
        Header string for MIME content type.
        See Also:
        Constant Field Values
    • Constructor Detail

      • HttpServer

        public HttpServer​(java.net.ServerSocket socket)
        Constructs a server based on a given socket.
        Parameters:
        socket - listening socket
      • HttpServer

        public HttpServer()
                   throws java.io.IOException
        Constructs a server based on a default socket, on any free port.
        Throws:
        java.io.IOException
    • Method Detail

      • addHandler

        public void addHandler​(HttpServer.Handler handler)
        Adds a handler which can serve some requests going through this server.
        Parameters:
        handler - handler to add
      • getSocket

        public java.net.ServerSocket getSocket()
        Returns the socket on which this server listens.
        Returns:
        server socket
      • getBaseUrl

        public java.net.URL getBaseUrl()
        Returns the base URL for this server.
        Returns:
        base URL
      • serve

        public HttpServer.Response serve​(HttpServer.Request request)
        Does the work for providing output corresponding to a given HTTP request. This implementation calls each Handler in turn and the first one to provide a non-null response is used.
        Parameters:
        request - represents an HTTP request that has been received
        Returns:
        represents the content of an HTTP response that should be sent
      • setDaemon

        public void setDaemon​(boolean isDaemon)
        Determines whether the server thread will be a daemon thread or not. Must be called before start() to have an effect. The default is true.
        Parameters:
        isDaemon - whether server thread will be daemon
        See Also:
        Thread.setDaemon(boolean)
      • start

        public void start()
        Starts the server if it is not already started.
      • stop

        public void stop()
        Stops the server if it is currently running. Processing of any requests which have already been received is completed.
      • isRunning

        public boolean isRunning()
        Indicates whether this server is currently running.
        Returns:
        true if running
      • serveRequest

        protected void serveRequest​(java.net.Socket sock)
                             throws java.io.IOException
        Called by the server thread for each new connection.
        Parameters:
        sock - client connection socket
        Throws:
        java.io.IOException
      • getHeader

        public static java.lang.String getHeader​(java.util.Map headerMap,
                                                 java.lang.String key)
        Returns a header value from a header map. Key value is case-insensitive. In the (undesirable) case that multiple keys with the same case-insensitive value exist, the values are concatenated with comma separators, as per RFC2616 section 4.2.
        Parameters:
        headerMap - map
        key - header key
        Returns:
        value of map entry with case-insensitive match for key
      • createErrorResponse

        public static HttpServer.Response createErrorResponse​(int code,
                                                              java.lang.String phrase)
        Utility method to create an error response.
        Parameters:
        code - status code
        phrase - status phrase
        Returns:
        new response object
      • create405Response

        public static HttpServer.Response create405Response​(java.lang.String[] supportedMethods)
        Creates an HTTP response indicating that the requested method (GET, POST, etc) is not supported.
        Parameters:
        supportedMethods - list of the methods which are supported
        Returns:
        error response
      • createErrorResponse

        public static HttpServer.Response createErrorResponse​(int code,
                                                              java.lang.String phrase,
                                                              java.lang.Throwable e)
        Utility method to create an error response given an exception.
        Parameters:
        code - status code
        phrase - status phrase
        e - exception which caused the trouble
        Returns:
        new response object