• Stars
    star
    354
  • Rank 120,042 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created over 1 year ago
  • Updated 2 months ago

Reviews

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

Repository Details

YetAnotherHttpHandler brings the power of HTTP/2 (and gRPC) to Unity and .NET Standard.

YetAnotherHttpHandler

YetAnotherHttpHandler brings the power of HTTP/2 to Unity and .NET Standard.

This library enables the use of HTTP/2, which Unity does not support. It allows you to use grpc-dotnet instead of the deprecated C-core gRPC library. It can also be used for asset downloading via HTTP/2, providing more functionality to your projects.

The library is implemented as a HttpHandler for HttpClient, so you can use the same API by just replacing the handler. (drop-in replacement)

Highlights

  • Unity support (including Apple Silicon support)
  • Compatible with gRPC (grpc-dotnet)
  • Leveraging System.Net.Http.HttpClient API

Under the hood

The handler is built on top of hyper and Rustls. It is a binding library that brings the power of those libraries to Unity and .NET.

Supported platforms and runtimes

  • Unity 2021.3 (LTS) or later
Architecture/Platform Windows macOS Linux Android iOS
x64 (x86_64) โœ” โœ” โ˜ โ˜ โ˜
arm64 (aarch64, Apple Silicon) ๐Ÿ“† โœ” ๐Ÿ“† โœ” โœ”
armv7 - - - โ˜ -
  • โœ” (Tier 1): Verify that it works, and active development support.
  • โ˜ (Tier 2): The build is available, but not confirmed to work.
  • ๐Ÿ“† (Planned): Currently, the build is not available yet.

Features

  • HTTP/1.0, HTTP/1.1
  • HTTP/2
    • Multiple streams on a single connection
    • Compatible with grpc-dotnet (Grpc.Net.Client)
    • HTTP/2 over cleartext
    • TLS 1.2 with ALPN
      • TLS support is powered by Rustls + webpki

Not supported yet

  • Client certificate
  • HTTP proxy support
  • Verification of certificates by security features built into the OS
  • More platform supports
    • Windows on Arm
    • Linux on Arm

Not supported (not planned)

  • NTLM and Kerberos authentication
  • Platforms
    • Unity 2021.2 or earlier
    • .NET 5+
    • 32bit architectures (x86)
    • tvOS, watchOS, Tizen
      • WebGL

Installation

Unity

This library depends on the following additional libraries:

Method 1: Using NuGetForUnity

  1. Install NuGetForUnity to your Unity project
  2. Install the following NuGet packages via NuGetForUnity:
    • System.IO.Pipelines
    • System.Runtime.CompilerServices.Unsafe
  3. Specify the following URL in Add package from git URL... of Package Manager on Unity
https://github.com/Cysharp/YetAnotherHttpHandler.git?path=src/YetAnotherHttpHandler#v1.0.0

Method 2: Using UnityNuGet scope registry

  1. Add the UnityNuget scope registry:
https://github.com/xoofx/UnityNuGet#add-scope-registry-manifestjson
  1. Specify the following URL in Add package from git URL... of Package Manager on Unity. This is the package that resolves NuGet dependencies.
https://github.com/Cysharp/YetAnotherHttpHandler.git?path=src/YetAnotherHttpHandler.Dependencies#v1.0.0
  1. Specify the following URL in Add package from git URL... of Package Manager on Unity. This is the main packagee.
https://github.com/Cysharp/YetAnotherHttpHandler.git?path=src/YetAnotherHttpHandler#v1.0.0

Method 3: Install from GitHub / Git repository

  1. Please download and install Cysharp.Net.Http.YetAnotherHttpHandler.Dependencies.unitypackage from the dependency redistribution on the release page.

  2. Specify the following URL in Add package from git URL... of Package Manager on Unity.

https://github.com/Cysharp/YetAnotherHttpHandler.git?path=src/YetAnotherHttpHandler#v1.0.1

Usage

Create an instance of YetAnotherHttpHandler and pass it to HttpClient.

using Cysharp.Net.Http;

using var handler = new YetAnotherHttpHandler();
var httpClient = new HttpClient(handler);

var result = await httpClient.GetStringAsync("https://www.example.com");

With these steps, your HttpClient is now compatible with HTTP/2.โœจ

YetAnotherHttpHandler and HttpClient can be held following best practices and shared across multiple threads or requests.

However, since it does not have features such as connection control by the number of streams, it is necessary to separate handler instances when explicitly creating different connections.

Using gRPC (grpc-dotnet) library

To use grpc-dotnet (Grpc.Net.Client), add the following additional libraries:

  • Grpc.Core.Api
  • Grpc.Net.Client
  • Grpc.Net.Common
  • Microsoft.Extensions.Logging.Abstractions
  • System.Buffers
  • System.Diagnostics.DiagnosticSource
  • System.Memory
  • System.Numerics.Vectors

Method 1: Using NuGetForUnity

  1. Install Grpc.Net.Client from NuGet via NuGetForUnity

Method 2: Using UnityNuget scope registry

To use grpc-dotnet (Grpc.Net.Client), add the following additional library to manifest.json:

{
  "dependencies": {
    "org.nuget.grpc.net.client": "{version}"
  }
}

Note

Replace {version} with the latest version available in NuGet: https://www.nuget.org/packages/Grpc.Net.Client#versions-body-tab

Method 2: Install pre-built package or install the libraries manually.

Please download and install Grpc.Net.Client.Dependencies.unitypackage from the dependency redistribution on the release page, or obtain the library from NuGet.

Create an instance of YetAnotherHttpHandler and pass it to GrpcChannelOptions.HttpHandler property.

Using GrpcChannel with YetAnotherHttpHandler

using Cysharp.Net.Http;

using var handler = new YetAnotherHttpHandler();
using var channel = GrpcChannel.ForAddress("https://api.example.com", new GrpcChannelOptions() { HttpHandler = handler });
var greeter = new GreeterClient(channel);

var result = await greeter.SayHelloAsync(new HelloRequest { Name = "Alice" });

// -- OR --

using var channel = GrpcChannel.ForAddress("https://api.example.com", new GrpcChannelOptions() { HttpHandler = new YetAnotherHttpHandler(), DisposeHttpClient = true });
var greeter = new GreeterClient(channel);

var result = await greeter.SayHelloAsync(new HelloRequest { Name = "Alice" });

Configurations

When creating a YetAnotherHttpHandler instance, you can configure the following HTTP client settings.

Once the handler sends a request, these settings become immutable and cannot be changed.

Property Description
PoolIdleTimeout Gets or sets an optional timeout for idle sockets being kept-alive. Default is 90 seconds.
MaxIdlePerHost Gets or sets the maximum idle connection per host allowed in the pool. Default is usize::MAX (no limit).
Http2Only Gets or sets a value that indicates whether to force the use of HTTP/2.
SkipCertificateVerification Gets or sets a value that indicates whether to skip certificate verification.
RootCertificates Gets or sets a custom root CA. By default, the built-in root CA (Mozilla's root certificates) is used. See also https://github.com/rustls/webpki-roots.
ClientAuthCertificates Gets or sets a custom client auth key.
ClientAuthKey Gets or sets a custom client auth certificates.
Http2InitialStreamWindowSize Gets or sets the SETTINGS_INITIAL_WINDOW_SIZE option for HTTP2 stream-level flow control.
Http2InitialConnectionWindowSize Gets or sets the max connection-level flow control for HTTP2
Http2AdaptiveWindow Gets or sets whether to use an adaptive flow control. Enabling this will override the limits set in http2_initial_stream_window_size and http2_initial_connection_window_size.
Http2MaxFrameSize Gets or sets the maximum frame size to use for HTTP2.
Http2KeepAliveInterval Gets or sets an interval for HTTP2 Ping frames should be sent to keep a connection alive. Pass null to disable HTTP2 keep-alive. Default is currently disabled.
Http2KeepAliveTimeout Gets or sets a timeout for receiving an acknowledgement of the keep-alive ping. If the ping is not acknowledged within the timeout, the connection will be closed. Does nothing if http2_keep_alive_interval is disabled. Default is 20 seconds.
Http2KeepAliveWhileIdle Gets or sets whether HTTP2 keep-alive should apply while the connection is idle. If disabled, keep-alive pings are only sent while there are open request/responses streams. If enabled, pings are also sent when no streams are active. Does nothing if http2_keep_alive_interval is disabled. Default is false.
Http2MaxConcurrentResetStreams Gets or sets the maximum number of HTTP2 concurrent locally reset streams. See the documentation of h2::client::Builder::max_concurrent_reset_streams for more details. The default value is determined by the h2 crate.
Http2MaxSendBufferSize Gets or sets the maximum write buffer size for each HTTP/2 stream. Default is currently 1MB, but may change.

Most of them expose hyper client settings, so please check those as well.

Advanced

Using HTTP/2 over cleartext (h2c)

gRPC requires communication over HTTP/2, but when it's a non-HTTPS connection, it defaults to attempting a connection with HTTP/1, causing connection issues. In this case, setting the YetAnotherHttpHandle.Http2Only property to true allows for connections via HTTP/2 over cleartext (h2c).

using var handler = new YetAnotherHttpHandler() { Http2Only = true };

Using custom root certificates

Currently, YetAnotherHttpHandler uses Mozilla's root certificates derived from webpki as the root CA.

If you want to use a self-signed certificate or a certificate issued by your organization, you need to set a custom root CA. In this case, you can specify the root certificates in pem format to RootCertificates property.

var rootCerts = @"
-----BEGIN CERTIFICATE-----
MIIE9TCCAt2gAwIBAgIUUQ33LbUPwlgKXmzA77KmDbV2uYkwDQYJKoZIhvcNAQEL
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTIzMDcyNTAzNDYzNFoXDTMzMDcy
MjAzNDYzNFowFDESMBAGA1UEAwwJbG9jYWxob3N0MIICIjANBgkqhkiG9w0BAQEF
AA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RgpcuUYmYbPigHSI2qpsWbqMeQmIvS
+s7Tap3sQFCYIGCvSmOV4STY2JqxeWOGgR/xLZBpfJgdllfy1mpe7dHpi0kVTEdE
cC1pNeFDn8xYm2M61oGUYy+b035HqD9SfPsnHOFzwgwINuHdL4xjmT+HwAtW+WOj
105d+aIK55gOaJPGa7Pc7UMYtN7Oc/9hWfYti0MXnsyYfCNx6Fl7jtKs1AG2BbQd
sReZj7em23DBe75I4+DCcNWQg40HXsDo2h+z+Xk3SFb/gvHMtmzFudKCDIpD0PS5
gEXEzkKRg/++6iXGF16eBibZ8PED6416rGJz1Bo1YpXSyYCZG6oWwXZTg9fvDZX5
FfLnQACV02y2Gs74h23Yq+QmA30Ly5GPrR5SBRaNiCY1SS7gCAfRah9zJjvbXNGw
h0Eq48Dlw12GaUww3y05/IoAJxtHxZigdQ==
-----END CERTIFICATE-----
";
using var handler = new YetAnotherHttpHandler() { RootCertificates = rootCerts };

Ignore certificate validation errors

We strongly not recommend this, but in some cases, you may want to skip certificate validation when connecting via HTTPS. In this scenario, you can ignore certificate errors by setting the SkipCertificateVerification property to true.

Development

Build & Tests

To debug run in your local environment, you first need to build the native library. Please specify the target as follows when building.

cargo build --target x86_64-pc-windows-msvc

When debugging or running unit tests, the native library is loaded from the following directory.

  • native/targets/{arch}-{os}-{toolchain}/{debug,release}/{lib}yaha_native.{dll,so}

When creating a package, The following artifacts directory is used.

  • native/artifacts/{.NET RID}/{lib}yaha_native.{dll,so}

License

MIT License

This library depends on third-party components. Please refer to THIRD-PARTY-NOTICES for their licenses.

More Repositories

1

UniTask

Provides an efficient allocation free async/await integration for Unity.
C#
8,201
star
2

MagicOnion

Unified Realtime/API framework for .NET platform and Unity.
C#
3,838
star
3

MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
C#
3,288
star
4

R3

The new future of dotnet/reactive and UniRx.
C#
2,177
star
5

ZString

Zero Allocation StringBuilder for .NET and Unity.
C#
2,060
star
6

ConsoleAppFramework

Zero Dependency, Zero Overhead, Zero Reflection, Zero Allocation, AOT Safe CLI Framework powered by C# Source Generator.
C#
1,635
star
7

MasterMemory

Embedded Typed Readonly In-Memory Document Database for .NET and Unity.
C#
1,521
star
8

MessagePipe

High performance in-memory/distributed messaging pipeline for .NET and Unity.
C#
1,406
star
9

Ulid

Fast .NET C# Implementation of ULID for .NET and Unity.
C#
1,314
star
10

ZLogger

Zero Allocation Text/Structured Logger for .NET with StringInterpolation and Source Generator, built on top of a Microsoft.Extensions.Logging.
C#
1,262
star
11

SimdLinq

Drop-in replacement of LINQ aggregation operations extremely faster with SIMD.
C#
775
star
12

csbindgen

Generate C# FFI from Rust for automatically brings native code and C native library to .NET and Unity.
Rust
688
star
13

ObservableCollections

High performance observable collections and synchronized views, for WPF, Blazor, Unity.
C#
559
star
14

ProcessX

Simplify call an external process with the async streams in C# 8.0.
C#
453
star
15

UnitGenerator

C# Source Generator to create value-object, inspired by units of measure.
C#
330
star
16

RuntimeUnitTestToolkit

CLI/GUI Frontend of Unity Test Runner to test on any platform.
C#
300
star
17

AlterNats

An alternative high performance NATS client for .NET.
C#
284
star
18

NativeMemoryArray

Utilized native-memory backed array for .NET and Unity - over the 2GB limitation and support the modern API(IBufferWriter, ReadOnlySequence, scatter/gather I/O, etc...).
C#
276
star
19

StructureOfArraysGenerator

Structure of arrays source generator to make CPU Cache and SIMD friendly data structure for high-performance code in .NET and Unity.
C#
262
star
20

MagicPhysX

.NET PhysX 5 binding to all platforms(win, osx, linux) for 3D engine, deep learning, dedicated server of gaming.
Rust
258
star
21

PrivateProxy

Source Generator and .NET 8 UnsafeAccessor based high-performance strongly-typed private accessor for unit testing and runtime.
C#
239
star
22

KcpTransport

KcpTransport is a Pure C# implementation of RUDP for high-performance real-time network communication
C#
237
star
23

LogicLooper

A library for building server application using loop-action programming model on .NET.
C#
237
star
24

DFrame

Distributed load testing framework for .NET and Unity.
C#
223
star
25

Utf8StreamReader

Utf8 based StreamReader for high performance text processing.
C#
208
star
26

LitJWT

Lightweight, Fast JWT(JSON Web Token) implementation for .NET.
C#
199
star
27

Claudia

Unofficial Anthropic Claude API client for .NET.
C#
162
star
28

CsprojModifier

CsprojModifier performs additional processing when Unity Editor generates the .csproj.
C#
154
star
29

Utf8StringInterpolation

Successor of ZString; UTF8 based zero allocation high-peformance String Interpolation and StringBuilder.
C#
153
star
30

ValueTaskSupplement

Append supplemental methods(WhenAny, WhenAll, Lazy) to ValueTask.
C#
135
star
31

Kokuban

Simplifies styling strings in the terminal for .NET application.
C#
123
star
32

SlnMerge

SlnMerge merges the solution files when generating solution file by Unity Editor.
C#
114
star
33

GrpcWebSocketBridge

Yet Another gRPC over HTTP/1 using WebSocket implementation, primarily targets .NET platform.
C#
76
star
34

WebSerializer

Convert Object into QueryString/FormUrlEncodedContent for C# HttpClient REST Request.
C#
65
star
35

RandomFixtureKit

Fill random/edge-case value to target type for unit testing, supports both .NET Standard and Unity.
C#
46
star
36

Actions

41
star
37

DocfxTemplate

Patchworked DocFX v2 template for Cysharp
JavaScript
7
star
38

Multicaster

A framework for transparently invoking multiple instances or clients.
C#
5
star
39

com.unity.ide.visualstudio-backport

Backport of com.unity.ide.visualstudio to before Unity 2019.4.21
C#
1
star