A tiny HTTP server implementation, for fun and learning
A tiny HTTP server implementation, for fun and learning. It’s not not fully HTTP compliant, but can handle simple GET requests, and possibly more in the future. I built it as a little side project after reading a couple of chapters from Elliotte Harold’s Java Network Programming
Here’s a quick overview of the architecture.
.-------------------------------------------------------------------------------.
| Tiny HTTP Server |
| .-----------------. .------------------------. .------------------------. |
| | SocketServer | | Executor Pool | | SocketProcessor | |
| | | | | | socket -> [Request] | |
| | SocketProcessor ----> [ SocketProcessor ] | | socket -> [Response] | |
| | ^ | | [ SocketProcessor ] ------> | | |
| | | | .------------------------. | | | |
| | socket | | v | |
| | | | .------------------------. | [ RequestHandler ] | |
| | waits incoming | | RequestHandler <------- [ RequestHandler ] | |
| | connection | | | .------------------------. |
| | ^ | | Request / Response | |
| .------|----------. .-----------|------------. |
.--------|-------------------------|--------------------------------------------.
| |
^ V
HTTP request HTTP response
SocketServer
, binds to the local host and given port, and awaits for incoming connectionsSocketProcessor
and added to a local Executor
pool (i.e, thread pool)Executor
service eventually calls on the SocketProcessor
to handle the Socket
SocketProcessor
builds the underlying Request
and Response
for the given SocketRequestHandlers
, giving each a chance to handleRequest
/Response
RequestHandler
sends the response back to client after completing it’s requestRunning with no options, the server will use the following defaults: port=8000
, pool-size=10
, document-root=.
$ mvn compile
$ java -cp target/classes com.gnoht.ths.Server
or with options.
$ java -cp target/classes com.gnoht.ths.Server \
--port=8080
--document-root=/my/document/root
--pool-size=50