Package org.astrogrid.samp.httpd

# 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.Handler`s 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 final String`
`HDR_CONTENT_TYPE`
Header string for MIME content type.
`static final 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(ServerSocket socket)`
Constructs a server based on a given socket.

## Method Summary

Modifier and Type
Method
Description
`void`
`addHandler(HttpServer.Handler handler)`
Adds a handler which can serve some requests going through this server.
`static HttpServer.Response`
`create405Response(String[] supportedMethods)`
Creates an HTTP response indicating that the requested method (GET, POST, etc) is not supported.
`static HttpServer.Response`
`createErrorResponse(int code, String phrase)`
Utility method to create an error response.
`static HttpServer.Response`
`createErrorResponse(int code, String phrase, Throwable e)`
Utility method to create an error response given an exception.
`URL`
`getBaseUrl()`
Returns the base URL for this server.
`static String`
`getHeader(Map headerMap, String key)`
Returns a header value from a header map.
`ServerSocket`
`getSocket()`
Returns the socket on which this server listens.
`boolean`
`isRunning()`
Indicates whether this server is currently running.
`void`
`removeHandler(HttpServer.Handler handler)`
Removes a handler previously added by `addHandler(org.astrogrid.samp.httpd.HttpServer.Handler)`.
`HttpServer.Response`
`serve(HttpServer.Request request)`
Does the work for providing output corresponding to a given HTTP request.
`protected void`
`serveRequest(Socket sock)`
Called by the server thread for each new connection.
`void`
`setDaemon(boolean isDaemon)`
Determines whether the server thread will be a daemon thread or not.
`void`
`start()`
Starts the server if it is not already started.
`void`
`stop()`
Stops the server if it is currently running.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### HDR_CONTENT_TYPE

public static final String HDR_CONTENT_TYPE
Header string for MIME content type.

See Also:
  - Constant Field Values

### STATUS_OK

public static final int STATUS_OK
Status code for OK (200).

See Also:
  - Constant Field Values

## 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:
  - `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(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