Class HttpServer

java.lang.Object
org.astrogrid.samp.httpd.HttpServer
Direct Known Subclasses:
CorsHttpServer

public class HttpServer extends 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
  • Field Details

  • Constructor Details

    • HttpServer

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

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

    • addHandler

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

      public void removeHandler(HttpServer.Handler handler)
      Removes a handler previously added by addHandler(org.astrogrid.samp.httpd.HttpServer.Handler).
      Parameters:
      handler - handler to remove
    • getSocket

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

      public 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:
    • 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(Socket sock) throws IOException
      Called by the server thread for each new connection.
      Parameters:
      sock - client connection socket
      Throws:
      IOException
    • getHeader

      public static String getHeader(Map headerMap, 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, 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(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, String phrase, 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