Byrone.Xenia.Encoding
0.1.1
dotnet add package Byrone.Xenia.Encoding --version 0.1.1
NuGet\Install-Package Byrone.Xenia.Encoding -Version 0.1.1
<PackageReference Include="Byrone.Xenia.Encoding" Version="0.1.1" />
<PackageVersion Include="Byrone.Xenia.Encoding" Version="0.1.1" />
<PackageReference Include="Byrone.Xenia.Encoding" />
paket add Byrone.Xenia.Encoding --version 0.1.1
#r "nuget: Byrone.Xenia.Encoding, 0.1.1"
#:package Byrone.Xenia.Encoding@0.1.1
#addin nuget:?package=Byrone.Xenia.Encoding&version=0.1.1
#tool nuget:?package=Byrone.Xenia.Encoding&version=0.1.1
Xenia.Encoding
Helpers to find the preferred encoding from the request and encode data.
Usage
A request may send the Accept-Encoding header. The value of this header is a list of accepted encodings, each encoding
with its own weight defining the preference of the browser. By installing Xenia.Encoding, you can easily handle
finding the preferred encoding and using this encoding to encode your response. Please keep in mind that only the
response body should be encoded, not the headers.
Here's an example on how to get the right encoding and encode the response data:
using Byrone.Xenia;
using Byrone.Xenia.Utilities;
static IResponse RequestHandler(in Request request) =>
new Response();
private readonly struct Response : IResponse
{
public void Send(Socket client, in Request request)
{
var body = "<html><body>Hello World</body></html>"u8;
// Parse the `Accept-Encoding` header, if present
if (request.TryGetEncoding(out var encoding))
{
// Create a temporary stream to write encoded data to
var stream = new RentedMemoryStream(64);
// Encode the data to the stream and return a span containing the encoded data
var compressed = BodyEncoding.TryEncode(stream, encoding, EncodingTests.Body);
Debug.Assert(compressed);
var response = StringBuilder.Format(
stackalloc byte[128],
$"""
HTTP/1.1 200 OK
Content-Encoding: {encoding}
Content-Type: text/html
Server: xenia-test-server
"""
);
client.Send(response);
// Send the encoded data
client.Send(System.MemoryExtensions.AsSpan(stream.GetBuffer(), 0, (int)stream.Position));
// Don't forget to dispose and release the stream's resources!
stream.Dispose();
}
else
{
// Handle fallback...
}
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Byrone.Xenia (>= 0.1.1)
- JetBrains.Annotations (>= 2024.3.0-eap1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.1 | 182 | 10/29/2024 |