https://github.com/codemonstur/httpserver
https://github.com/codemonstur/httpserver
Last synced: 12 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/codemonstur/httpserver
- Owner: codemonstur
- License: mit
- Created: 2021-06-21T18:31:37.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-08-05T18:03:01.000Z (over 1 year ago)
- Last Synced: 2024-08-06T21:24:28.657Z (over 1 year ago)
- Language: Java
- Size: 148 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/codemonstur/httpserver/releases)
[](http://mvnrepository.com/artifact/com.github.codemonstur/httpserver)
[](https://opensource.org/licenses/mit-license.php)
# HTTP server
A standalone embedded HTTP server.
Features it has:
- Support for HTTP/0.9
- Support for HTTP/1.0
- Support for HTTP/1.1
- Common headers and status codes
- Parsing url encoded forms
- Parsing multipart forms
- Helpers for request parsing
- Helpers for response sending
- Session store
- Routing handler based on method and path
- Compression using gz and deflate
- Compilable with Graal
Missing features:
- No SSE
- No websockets
- No HTTP/2
## Usage
Add the maven dependency:
com.github.codemonstur
httpserver
1.5.0
Instantiate the server from somewhere (your main() probably):
HttpServer.newHttpServer()
.bind(8080, "0.0.0.0")
.handler(handler)
.build()
.start();
Optionally define some routes:
MethodPathRouting.methodPathRouting()
.put("/api/v1/endpoint", putEndpoint())
.post("/api/v1/endpoint", postEndpoint())
.get("/api/v1/endpoint", getEndpoint())
.delete("/api/v1/endpoint", deleteEndpoint())
Define a handler by implementing this interface:
public interface HttpHandler {
void handleRequest(HttpServerExchange exchange) throws Exception;
}
Throw an Exception, and the server will return a 500 Internal Server Error.
Parse your request and send your response using the HttpServerExchange object.
An example of a handler:
public static HttpHandler postEndpoint() {
return exchange -> {
final var form = parseForm(exchange, UTF_8);
final long id = getMandatoryLong(form, "id");
... do something here ...
respond(exchange).status(NO_CONTENT).send();
};
}
## Threading
We create a new virtual Thread for each incoming connection.
This should be sufficient to use in production.
## Testing
There is no test harness yet.
No benchmarks have been run.
## Security
- Form parsing has a Denial of Service issue.
- There are no limits on resource use.
- There is no protection against header injection
## Graal support
Graal doesn't support Java 19 yet.
This library isn't very complicated, it should just work by the time Graal support for virtual threads comes along.