项目作者: ikumen

项目描述 :
A tiny HTTP server implementation, for fun and learning
高级语言: Java
项目地址: git://github.com/ikumen/tiny-http-server.git
创建时间: 2020-01-23T18:03:52Z
项目社区:https://github.com/ikumen/tiny-http-server

开源协议:MIT License

下载


Tiny HTTP Server

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

Overview

Here’s a quick overview of the architecture.

  1. .-------------------------------------------------------------------------------.
  2. | Tiny HTTP Server |
  3. | .-----------------. .------------------------. .------------------------. |
  4. | | SocketServer | | Executor Pool | | SocketProcessor | |
  5. | | | | | | socket -> [Request] | |
  6. | | SocketProcessor ----> [ SocketProcessor ] | | socket -> [Response] | |
  7. | | ^ | | [ SocketProcessor ] ------> | | |
  8. | | | | .------------------------. | | | |
  9. | | socket | | v | |
  10. | | | | .------------------------. | [ RequestHandler ] | |
  11. | | waits incoming | | RequestHandler <------- [ RequestHandler ] | |
  12. | | connection | | | .------------------------. |
  13. | | ^ | | Request / Response | |
  14. | .------|----------. .-----------|------------. |
  15. .--------|-------------------------|--------------------------------------------.
  16. | |
  17. ^ V
  18. HTTP request HTTP response
  • upon start up the server creates a SocketServer, binds to the local host and given port, and awaits for incoming connections
  • each incoming connection (as a Socket) is given to a SocketProcessor and added to a local Executor pool (i.e, thread pool)
  • the Executor service eventually calls on the SocketProcessor to handle the Socket
  • the SocketProcessor builds the underlying Request and Response for the given Socket
    connection, and cycles through a list RequestHandlers, giving each a chance to handle
    the Request/Response
  • the RequestHandler sends the response back to client after completing it’s request

Running the server

Running with no options, the server will use the following defaults: port=8000, pool-size=10, document-root=.

  1. $ mvn compile
  2. $ java -cp target/classes com.gnoht.ths.Server

or with options.

  1. $ java -cp target/classes com.gnoht.ths.Server \
  2. --port=8080
  3. --document-root=/my/document/root
  4. --pool-size=50