public class HttpServer extends Object
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.
| Modifier and Type | Class and 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.
|
| Modifier and Type | Field and Description |
|---|---|
static String |
HDR_CONTENT_TYPE
Header string for MIME content type.
|
static int |
STATUS_OK
Status code for OK (200).
|
| Constructor and 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.
|
| 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(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.
|
public static final String HDR_CONTENT_TYPE
public static final int STATUS_OK
public HttpServer(ServerSocket socket)
socket - listening socketpublic HttpServer()
throws IOException
IOExceptionpublic 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 ServerSocket getSocket()
public 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(Socket sock) throws IOException
sock - client connection socketIOExceptionpublic static String getHeader(Map headerMap, String key)
headerMap - mapkey - header keypublic static HttpServer.Response createErrorResponse(int code, String phrase)
code - status codephrase - status phrasepublic static HttpServer.Response create405Response(String[] supportedMethods)
supportedMethods - list of the methods which are supportedpublic static HttpServer.Response createErrorResponse(int code, String phrase, Throwable e)
code - status codephrase - status phrasee - exception which caused the troubleCopyright © 2008–2024. All rights reserved.