Skip to content

codemonstur/httpserver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub Release Maven Central MIT Licence

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:

<dependency>
    <groupId>com.github.codemonstur</groupId>
    <artifactId>httpserver</artifactId>
    <version>1.1.0</version>
</dependency>

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.