• Stars
    star
    204
  • Rank 192,063 (Top 4 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 2 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A full featured, stand-alone, high-performance HTTP server and client written entirely in plain Java

FusionAuth HTTP client and server semver 2.0.0 compliant test

NOTE: This project is in progress.

The goal of this project is to build a full-featured HTTP server and client in plain Java without the use of any libraries. The client and server will use non-blocking NIO in order to provide the highest performance possible.

Installation

To add this library to your project, you can include this dependency in your Maven POM:

<dependency>
  <groupId>io.fusionauth</groupId>
  <artifactId>java-http</artifactId>
  <version>0.2.0</version>
</dependency>

If you are using Gradle, you can add this to your build file:

implementation 'io.fusionauth:java-http:0.2.0'

If you are using Savant, you can add this to your build file:

dependency(id: "io.fusionauth:java-http:0.2.0")

Examples Usages:

Creating a server is simple:

import io.fusionauth.http.server.HTTPListenerConfiguration;
import io.fusionauth.http.server.HTTPServer;
import io.fusionauth.http.server.HTTPHandler;

public class Example {
  public static void main(String... args) {
    HTTPHandler handler = (req, res) -> {
      // Handler code goes here
    };

    HTTPServer server = new HTTPServer().withHandler(handler).withListener(new HTTPListenerConfiguration(4242));
    server.start();
    // Use server
    server.close();
  }
}

Since the HTTPServer class implements java.io.Closeable, you can also use a try-resource block like this:

import io.fusionauth.http.server.HTTPListenerConfiguration;
import io.fusionauth.http.server.HTTPServer;
import io.fusionauth.http.server.HTTPHandler;

public class Example {
  public static void main(String... args) {
    HTTPHandler handler = (req, res) -> {
      // Handler code goes here
    };

    try (HTTPServer server = new HTTPServer().withHandler(handler).withListener(new HTTPListenerConfiguration(4242))) {
      server.start();
      // When this block exits, the server will be shutdown
    }
  }
}

You can also set various options on the server using the with methods on the class like this:

import java.time.Duration;

import io.fusionauth.http.server.HTTPListenerConfiguration;
import io.fusionauth.http.server.HTTPServer;
import io.fusionauth.http.server.HTTPHandler;

public class Example {
  public static void main(String... args) {
    HTTPHandler handler = (req, res) -> {
      // Handler code goes here
    };

    HTTPServer server = new HTTPServer().withHandler(handler)
                                        .withNumberOfWorkerThreads(42)
                                        .withShutdownDuration(Duration.ofSeconds(10L))
                                        .withListener(new HTTPListenerConfiguration(4242));
    server.start();
    // Use server
    server.close();
  }
}

TLS

The HTTP server implements TLS 1.0-1.3 using the Java SSLEngine. To enable TLS for your server, you need to create an HTTPListenerConfiguration that includes a certificate and private key. Most production use-cases will use a proxy such as Apache, Nginx, ALBs, etc. In development, it is recommended that you set up self-signed certificates and load those into the HTTP server.

To set up self-signed certificates on macOS, you can use the program mkcert. Here is an example:

brew install mkcert
mkcert -install
mkdir -p ~/dev/certificates
mkcert -cert-file ~/dev/certificates/example.org.pem -key-file ~/dev/certificates/example.org.key example.org

In production environments, your certificate will likely be signed by one or more intermediate Certificate Authorities. In addition to the server certificate, ensure that all intermediate CA certificates in the chain are included in your pem file.

Now you can load these into the HTTP server like this:

import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Duration;

import io.fusionauth.http.server.HTTPHandler;
import io.fusionauth.http.server.HTTPServer;

public class Example {
  private String certificate;

  private String privateKey;

  public static void main(String[] args) {
    String homeDir = System.getProperty("user.home");
    certificate = Files.readString(Paths.get(homeDir + "/dev/certificates/example.org.pem"));
    privateKey = Files.readString(Paths.get(homeDir + "/dev/certificates/example.org.key"));

    HTTPHandler handler = (req, res) -> {
      // Handler code goes here
    };

    HTTPServer server = new HTTPServer().withHandler(handler)
                                        .withListener(new HTTPListenerConfiguration(4242, certificate, privateKey));
    // Use server
    server.close();
  }
}

And finally, you'll need to add the domain name to your hosts file to ensure that the SNI lookup handles the certificate correctly. For this example, you would use this entry in the /etc/hosts file:

127.0.0.1 example.org

Then you can open https://example.org in a browser or call it using an HTTP client (i.e. Insomnia, Postman, etc or in code).

Performance

A key component for this project is to have awesome performance. Here are some basic metrics using the FusionAuth load test suite against a simple application using the Prime Framework MVC. The controller does nothing except return a simple 200. Here are some simple comparisons between Tomcat, Netty, and java-http.

The load test configuration is set to 10 clients with 500,000 requests each. The client is Restify which is a FusionAuth library that uses HttpURLConnection under the hoods. All the servers were HTTP so that TLS would not introduce any additional latency.

Here are the current test results:

Server RPS Failures per second
java-http 63,216 0
Tomcat 51,351 0.103
Netty 540 1.818

Netty and Tomcat both seem to suffer from buffering and connection issues at very high scale. Regardless of the configuration, both servers always begins to fail with connection timeout problems at scale. java-http does not have these issues because of the way it handles connections via the selector. Connections don't back up and client connection pools can always be re-used with Keep-Alive.

The general requirements and roadmap are as follows:

Todos and Roadmap

Server tasks

  • Basic HTTP 1.1
  • Support Keep-Alive
  • Support Expect-Continue 100
  • Support chunked request
  • Support chunked response
  • Support streaming entity bodies (via chunking likely)
  • Support compression (default and per response options)
  • Support cookies in request and response
  • Clean up HTTPRequest
  • Support form data
  • Support multipart form data
  • Support TLS
  • Support trailers
  • Support HTTP 2

Client tasks

  • Basic HTTP 1.1
  • Support Keep-Alive
  • Support TLS
  • Support Expect-Continue 100
  • Support chunked request and response
  • Support streaming entity bodies
  • Support form data
  • Support multipart form data
  • Support HTTP 2

FAQ

Why no Loom?

Project Loom is an exciting development which brings a lot of great new features to Java, such as fibers, continuations and more.

Loom is currently available in Java 19 as a preview feature. Therefore, you can't use it without compiled code that is difficult to use in future Java releases.

This project is anchored to the Java LTS releases to ensure compatibility. Loom will be evaluated once it is out of preview, and available in an LTS version of Java.

The next scheduled LTS release will be Java 21 set to release in September 2023. We are looking forward to that release and to see if we can leverage the Loom features in this project.

Helping out

We are looking for Java developers that are interested in helping us build the client and server. If you know a ton about networks and protocols and love writing clean, high-performance Java, contact us at [email protected].

Building with Savant

Note: This project uses the Savant build tool. To compile using Savant, follow these instructions:

$ mkdir ~/savant
$ cd ~/savant
$ wget http://savant.inversoft.org/org/savantbuild/savant-core/2.0.0-RC.6/savant-2.0.0-RC.6.tar.gz
$ tar xvfz savant-2.0.0-RC.6.tar.gz
$ ln -s ./savant-2.0.0-RC.6 current
$ export PATH=$PATH:~/savant/current/bin/

More Repositories

1

security-scripts

Scripts built from our Guide to User Data Security
Shell
442
star
2

fusionauth-containers

Container definitions for docker, kubernetes, helm, and whatever containers come next!
Dockerfile
196
star
3

fusionauth-jwt

A simple to use Java 8 JWT Library. Verify, Sign, Encode, Decode all day.
Java
143
star
4

fusionauth-issues

FusionAuth issue submission project
91
star
5

fusionauth-install

FusionAuth simple install scripts. Copy, Paste, Code.
PowerShell
68
star
6

fusionauth-typescript-client

A TypeScript client for FusionAuth
TypeScript
52
star
7

fusionauth-site

Website and documentation for FusionAuth
MDX
49
star
8

terraform-provider-fusionauth

FusionAuth Terraform Provider
Go
34
star
9

fusionauth-example-modern-guide-to-oauth

The example application paired with the Modern Guide to OAuth
CSS
26
star
10

fusionauth-netcore-client

The .NET Core client for FusionAuth
C#
24
star
11

fusionauth-node-client

Node.js client library for FusionAuth
JavaScript
23
star
12

charts

Public helm charts
Mustache
23
star
13

go-client

FusionAuth Go Client Library!
Go
22
star
14

fusionauth-example-react

This simple example app shows how you can use FusionAuth in a React app to log in, log out, and manipulate user data.
JavaScript
20
star
15

fusionauth-php-client

PHP client library for FusionAuth
PHP
19
star
16

webauthn.wtf

The webauthn.wtf website
Astro
19
star
17

fusionauth-python-client

FusionAuth Python Client
Python
19
star
18

fusionauth-example-symfony-multitenant

An example multi tenant application.
PHP
16
star
19

fusionauth-nodejs-react-example

FusionAuth Example for Node.js and React
JavaScript
13
star
20

fusionauth-example-spring-security

An example usage of the fusionauth-spring-security project
Java
12
star
21

fusionauth-ruby-client

Ruby client library for FusionAuth
Ruby
11
star
22

fusionauth-localization

FusionAuth translations
Ruby
11
star
23

fusionauth-example-go-jwt-microservices

Example of a golang API gateway with authorization using JWT
Go
10
star
24

fusionauth-react-sdk

An SDK for using FusionAuth with React
TypeScript
10
star
25

fusionauth-import-scripts

FusionAuth Import scripts for Auth0 and other examples
Ruby
10
star
26

fusionauth-java-client

Java 8 client library for FusionAuth
Java
10
star
27

fusionauth-contrib

Community contributed examples and code
Java
9
star
28

fusionauth-dart-client

A Dart client for FusionAuth, Flutter compatible
Dart
9
star
29

fusionauth-javascript-sdk

Javascript SDK for FusionAuth
TypeScript
9
star
30

fusionauth-javascript-client

JavaScript client library for FusionAuth
JavaScript
8
star
31

fusionauth-example-node

Node.js example application that uses the OAuth 2 Authorization Code grant
JavaScript
8
star
32

fusionauth-openapi

FusionAuth OpenAPI client
Shell
7
star
33

fusionauth-example-vue

Vue.js and Express example application that uses the OAuth 2 Authorization Code grant
JavaScript
7
star
34

fusionauth-example-flask-portal

A user portal written in python/flask, using FusionAuth as the user data store
Python
7
star
35

fusionauth-theme-helper

Scripts to help update FusionAuth themes
Shell
6
star
36

fusionauth-example-node-services-gateway-jwtauth

Using JWT Auth for node microservices
JavaScript
6
star
37

fusionauth-example-laravel-single-sign-on

Sample application from the "Single sign-on with Laravel and FusionAuth" article
PHP
6
star
38

fusionauth-example-kickstart

Example Kickstart files
FreeMarker
6
star
39

fusionauth-example-python-flask

Sample flask application using FusionAuth for OAuth/OIDC
Python
6
star
40

fusionauth-2fa

Two Factor Helper for RFC4226 HMAC-Based One-Time Password Algorithm
JavaScript
6
star
41

fusionauth-example-go

Go
5
star
42

fusionauth-client-builder

The FusionAuth client library builder
FreeMarker
5
star
43

fusionauth-example-python-django

Python
5
star
44

fusionauth-samlv2

SAML v2.0 bindings in Java using JAXB
Java
5
star
45

fusionauth-example-flutter-dart

Dart
5
star
46

fusionauth-example-react-native-0-71

Java
5
star
47

fusionauth-quickstart-python-flask-web

CSS
5
star
48

fusionauth-astro-components

Astro component to pull in remote values or code at build time
Astro
5
star
49

fusionauth-spring-security

FusionAuth OpenID Connect Library for Spring Security
Java
4
star
50

fusionauth-example-rails-oauth

Example securing Rails app using FusionAuth
Ruby
4
star
51

fusionauth-example-react-native

demo for tutorial How to use FusionAuth with React native
Java
4
star
52

omniauth-fusionauth

Ruby OmniAuth Strategy for FusionAuth
Ruby
4
star
53

fusionauth-example-angular

Example of integrating Angular with FusionAuth via OAuth Authorization Code Grant
TypeScript
4
star
54

fusionauth-example-node-sso

Example of single sign-on with FusionAuth and node
JavaScript
4
star
55

fusionauth-example-react-2.0

JavaScript
4
star
56

fusionauth-csharp-client

C# client library for FusionAuth
C#
4
star
57

fusionauth-example-go-jwt

JWT manipulation in golang
Go
3
star
58

fusionauth-example-django-single-sign-on

Single sign-on with django and FusionAuth
Python
3
star
59

fusionauth-example-node-services-gateway

A FusionAuth-powered API gateway with microservices example
JavaScript
3
star
60

fusionauth-load-tests

FusionAuth load tests
Java
3
star
61

fusionauth-angular-example

Angular7 example integration with FusionAuth
TypeScript
3
star
62

fusionauth-javascript-sdk-express

JavaScript
3
star
63

fusionauth-quickstart-dotnet-web

CSS
3
star
64

fusionauth-quickstart-dotnet-api

CSS
3
star
65

fusionauth-android-sdk

Android SDK for FusionAuth
CSS
3
star
66

fusionauth-example-node-multi-tenant

Multi tenant applications with FusionAuth
JavaScript
3
star
67

fusionauth-example-scripts

Example script which pulls all user data iterating by email prefix.
Shell
2
star
68

gdpr.js

JavaScript library for handling those pesky GDPR rules
JavaScript
2
star
69

fusionauth-example-java-spring

FusionAuth, Spring and OIDC example application
Java
2
star
70

fusionauth-example-password-encryptor

A example you can use to build a Password Encryptor Plugin for FusionAuth
Java
2
star
71

fusionauth-android-client

FusionAuth Android Client
Java
2
star
72

fusionauth-ebooks

Source for ebooks
2
star
73

fusionauth-example-go-device-code-grant

get-gif: An Example Golang CLI app using the FusionAuth Golang Client Library to provide Device Code OAuth
Go
2
star
74

fusionauth-example-java-jwt

Java JWT usage
Java
2
star
75

fusionauth-oauth1

Helper to build an OAuth v1 authorization header for Twitter or other OAuth v1 services.
Java
2
star
76

fusionauth-example-python-hotspot

Control a hotspot with FusionAuth
Python
2
star
77

fusionauth-example-rails-app

Ruby
2
star
78

fusionauth-example-rails-api

A rails API using JWT authentication
Ruby
2
star
79

fusionauth-example-python-jwt

JWT examples in python
Python
2
star
80

fusionauth-example-javascript-jwt

Example of JavaScript JWT manipulation
JavaScript
2
star
81

fusionauth-theme-history

Historical theme files
FreeMarker
2
star
82

homebrew-fusionauth

macOS Homebrew tap for FusionAuth
Ruby
2
star
83

fusionauth-example-react-sdk

Example of using FusionAuth with the react SDK.
CSS
2
star
84

fusionauth-quickstart-javascript-react-web

CSS
2
star
85

fusionauth-quickstart-javascript-angular-web

CSS
2
star
86

fusionauth-web-template

Web template for use with FusionAuth websites
HTML
2
star
87

fusionauth-quickstart-python-django-web

CSS
2
star
88

fusionauth-quickstart-react-native

Sample application for the React Native quickstart app
CSS
2
star
89

fusionauth-quickstart-golang-api

The quickstart for Go API
Go
2
star
90

fusionauth-example-cross-platform-game

An example of a cross platform game built using FusionAuth and dart
C++
1
star
91

fusionauth-example-laravel

A demo using FusionAuth to manage user authentication in Laravel
PHP
1
star
92

fusionauth-example-express-twitter

Example of using passport.js and FusionAuth to add login with twitter to your app.
JavaScript
1
star
93

fusionauth-swift-client

iOS Swift client library for FusionAuth
Swift
1
star
94

fusionauth-example-php-connector

Example application for use with a generic Connector.
PHP
1
star
95

fusionauth-theme-management

Shell
1
star
96

fusionauth-example-asp-netcore

An ASP.NET Core web application using FusionAuth as the identity server
C#
1
star
97

fusionauth-example-supabase

Sample application demonstrating how to use Supabase in a Next.js application with FusionAuth
JavaScript
1
star
98

fusionauth-angular-client

TypeScript
1
star
99

fusionauth-example-5-minute-guide

The 5 minute guide codebase
JavaScript
1
star
100

fusionauth-example-java

An example of using the java client
Java
1
star