Package org.astrogrid.samp.httpd
Class HttpServer
java.lang.Object
org.astrogrid.samp.httpd.HttpServer
- Direct Known Subclasses:
CorsHttpServer
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 ClassesModifier and TypeClassDescriptionstatic interface
Implemented to serve data for some URLs.static class
Represents a parsed HTTP client request.static class
Represents a response to an HTTP request. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Header string for MIME content type.static final int
Status code for OK (200). -
Constructor Summary
ConstructorsConstructorDescriptionConstructs 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 TypeMethodDescriptionvoid
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.Returns the base URL for this server.static String
Returns a header value from a header map.Returns the socket on which this server listens.boolean
Indicates whether this server is currently running.void
removeHandler
(HttpServer.Handler handler) Removes a handler previously added byaddHandler(org.astrogrid.samp.httpd.HttpServer.Handler)
.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.
-
Field Details
-
HDR_CONTENT_TYPE
Header string for MIME content type.- See Also:
-
STATUS_OK
public static final int STATUS_OKStatus code for OK (200).- See Also:
-
-
Constructor Details
-
HttpServer
Constructs a server based on a given socket.- Parameters:
socket
- listening socket
-
HttpServer
Constructs a server based on a default socket, on any free port.- Throws:
IOException
-
-
Method Details
-
addHandler
Adds a handler which can serve some requests going through this server.- Parameters:
handler
- handler to add
-
removeHandler
Removes a handler previously added byaddHandler(org.astrogrid.samp.httpd.HttpServer.Handler)
.- Parameters:
handler
- handler to remove
-
getSocket
Returns the socket on which this server listens.- Returns:
- server socket
-
getBaseUrl
Returns the base URL for this server.- Returns:
- base URL
-
serve
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 beforestart()
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
Called by the server thread for each new connection.- Parameters:
sock
- client connection socket- Throws:
IOException
-
getHeader
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
- mapkey
- header key- Returns:
- value of map entry with case-insensitive match for key
-
createErrorResponse
Utility method to create an error response.- Parameters:
code
- status codephrase
- status phrase- Returns:
- new response object
-
create405Response
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
Utility method to create an error response given an exception.- Parameters:
code
- status codephrase
- status phrasee
- exception which caused the trouble- Returns:
- new response object
-