public class HttpServer
extends java.lang.Object
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.
Modifier and Type | Class and Description |
---|---|
static interface |
HttpServer.Handler
Implemented to serve data for some URLs.
|
private static class |
HttpServer.HttpException
Convenience class for representing an error whose content should be
returned to the user as an HTTP erro response of some kind.
|
(package private) static class |
HttpServer.HttpHeaderMap
Map implementation suitable for storing HTTP headers.
|
static class |
HttpServer.Request
Represents a parsed HTTP client request.
|
static class |
HttpServer.Response
Represents a response to an HTTP request.
|
Modifier and Type | Field and Description |
---|---|
private java.net.URL |
baseUrl_ |
private java.util.List |
handlerList_ |
private static java.lang.String |
HDR_CONTENT_LENGTH |
static java.lang.String |
HDR_CONTENT_TYPE
Header string for MIME content type.
|
private static java.util.regex.Pattern |
HEADER_PATTERN |
private static java.lang.String |
HTTP_TOKEN_REGEX |
private static java.lang.String |
HTTP_VERSION_REGEX |
private boolean |
isDaemon_ |
private static java.util.logging.Logger |
logger_ |
private static java.util.regex.Pattern |
REQUEST_LINE_PATTERN |
private java.net.ServerSocket |
serverSocket_ |
private static java.util.regex.Pattern |
SIMPLE_REQUEST_PATTERN |
private boolean |
started_ |
static int |
STATUS_OK
Status code for OK (200).
|
private boolean |
stopped_ |
private static java.lang.String |
URI_REGEX |
Constructor and 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.
|
Modifier and Type | Method and Description |
---|---|
void |
addHandler(HttpServer.Handler handler)
Adds a handler which can serve some requests going through this server.
|
static HttpServer.Response |
create405Response(java.lang.String[] supportedMethods)
Creates an HTTP response indicating that the requested method
(GET, POST, etc) is not supported.
|
static HttpServer.Response |
createErrorResponse(int code,
java.lang.String phrase)
Utility method to create an error response.
|
static HttpServer.Response |
createErrorResponse(int code,
java.lang.String phrase,
java.lang.Throwable e)
Utility method to create an error response given an exception.
|
java.net.URL |
getBaseUrl()
Returns the base URL for this server.
|
static java.lang.String |
getHeader(java.util.Map headerMap,
java.lang.String key)
Returns a header value from a header map.
|
java.net.ServerSocket |
getSocket()
Returns the socket on which this server listens.
|
boolean |
isRunning()
Indicates whether this server is currently running.
|
private static HttpServer.Request |
parseRequest(java.io.InputStream in,
java.net.SocketAddress remoteAddress)
Takes the input stream from a client connection and turns it into
a Request object.
|
private static java.lang.String[] |
readHeaderLines(java.io.InputStream is)
Reads the header lines from an HTTP client connection input stream.
|
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(java.net.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.
|
private final java.net.ServerSocket serverSocket_
private boolean isDaemon_
private java.util.List handlerList_
private final java.net.URL baseUrl_
private volatile boolean started_
private volatile boolean stopped_
public static final java.lang.String HDR_CONTENT_TYPE
private static final java.lang.String HDR_CONTENT_LENGTH
public static final int STATUS_OK
private static final java.lang.String URI_REGEX
private static final java.lang.String HTTP_VERSION_REGEX
private static final java.lang.String HTTP_TOKEN_REGEX
private static final java.util.regex.Pattern SIMPLE_REQUEST_PATTERN
private static final java.util.regex.Pattern REQUEST_LINE_PATTERN
private static final java.util.regex.Pattern HEADER_PATTERN
private static final java.util.logging.Logger logger_
public HttpServer(java.net.ServerSocket socket)
socket
- listening socketpublic HttpServer() throws java.io.IOException
java.io.IOException
public void addHandler(HttpServer.Handler handler)
handler
- handler to addpublic void removeHandler(HttpServer.Handler handler)
addHandler(org.astrogrid.samp.httpd.HttpServer.Handler)
.handler
- handler to removepublic java.net.ServerSocket getSocket()
public java.net.URL getBaseUrl()
public HttpServer.Response serve(HttpServer.Request request)
request
- represents an HTTP request that has been receivedpublic void setDaemon(boolean isDaemon)
start()
to have an effect.
The default is true.isDaemon
- whether server thread will be daemonThread.setDaemon(boolean)
public void start()
public void stop()
public boolean isRunning()
protected void serveRequest(java.net.Socket sock) throws java.io.IOException
sock
- client connection socketjava.io.IOException
private static HttpServer.Request parseRequest(java.io.InputStream in, java.net.SocketAddress remoteAddress) throws java.io.IOException
in
- input streamremoteAddress
- address of requesting clientjava.io.IOException
private static java.lang.String[] readHeaderLines(java.io.InputStream is) throws java.io.IOException
is
- socket input streamjava.io.IOException
public static java.lang.String getHeader(java.util.Map headerMap, java.lang.String key)
headerMap
- mapkey
- header keypublic static HttpServer.Response createErrorResponse(int code, java.lang.String phrase)
code
- status codephrase
- status phrasepublic static HttpServer.Response create405Response(java.lang.String[] supportedMethods)
supportedMethods
- list of the methods which are supportedpublic static HttpServer.Response createErrorResponse(int code, java.lang.String phrase, java.lang.Throwable e)
code
- status codephrase
- status phrasee
- exception which caused the trouble