Package edu.rit.http

Class HttpServer

java.lang.Object
edu.rit.http.HttpServer

public abstract class HttpServer extends Object
Class HttpServer provides a lightweight HTTP/1.0 server. The HTTP server is designed to be embedded inside another application.

When constructed, the HTTP server starts a thread listening for connections to a given host and port. When a web browser sets up a connection, the HTTP server calls the process() method to process the request. This is an abstract method that must be overridden in a subclass. The process() method's arguments are an HttpRequest object from which the method reads the HTTP request message and an HttpResponse object to which the method writes the HTTP response message.

The HTTP server assumes that the process() method will not perform any lengthy processing and will not block the calling thread. If the HTTP request requires lengthy processing or requires blocking, the process() method should spawn a separate thread to process the request. A thread pool may prove useful; see package java.util.concurrent.

When a client opens a socket connection to the HTTP server, the server places a two-second timeout on reading the socket. If the client does not send an HTTP request message before the timeout, the HttpRequest's isValid() method returns false.

Version:
29-Jul-2010
Author:
Alan Kaminsky
  • Constructor Details

    • HttpServer

      public HttpServer(InetSocketAddress address) throws IOException
      Construct a new HTTP server. The HTTP server will print error messages on the standard error.
      Parameters:
      address - Host and port to which the HTTP server will listen for connections.
      Throws:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • HttpServer

      public HttpServer(InetSocketAddress address, Logger logger) throws IOException
      Construct a new HTTP server. The HTTP server will print error messages using the given logger.
      Parameters:
      address - Host and port to which the HTTP server will listen for connections.
      logger - Error message logger. If null, the HTTP server will print error messages on the standard error.
      Throws:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
  • Method Details

    • getAddress

      public InetSocketAddress getAddress()
      Obtain the host and port to which this HTTP server is listening for connections.
      Returns:
      Host and port.
    • close

      public void close() throws IOException
      Close this HTTP server.
      Throws:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • process

      protected abstract void process(HttpRequest theRequest, HttpResponse theResponse) throws IOException
      Process the given HTTP request. The process() method must be overridden in a subclass to read the HTTP request from theRequest and write the HTTP response to theResponse.
      Parameters:
      theRequest - HTTP request.
      theResponse - HTTP response.
      Throws:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.