Byrone.Xenia.Data 0.1.1

dotnet add package Byrone.Xenia.Data --version 0.1.1
                    
NuGet\Install-Package Byrone.Xenia.Data -Version 0.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Byrone.Xenia.Data" Version="0.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Byrone.Xenia.Data" Version="0.1.1" />
                    
Directory.Packages.props
<PackageReference Include="Byrone.Xenia.Data" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Byrone.Xenia.Data --version 0.1.1
                    
#r "nuget: Byrone.Xenia.Data, 0.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Byrone.Xenia.Data@0.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Byrone.Xenia.Data&version=0.1.1
                    
Install as a Cake Addin
#tool nuget:?package=Byrone.Xenia.Data&version=0.1.1
                    
Install as a Cake Tool

Xenia.Data

Collection of helpers/utilities for working with different kinds of data.

Chunked data

Also known as Transfer-Encoding: chunked, this is a way of sending data when the sender is not quite sure what the content length is yet.

The idea behind this is that data is sent in 'chunks' (hence the chunked directive). A chunk looks something like this:

1D # A hex value representing the chunk's size (in this example, 29).
This is some chunked content. # The chunks content

When a request's (or response, but mostly requests use this) Transfer-Encoding header is set to chunked, the request body is a list of chunks like this. At the end of the list, there's an empty list with a length of 0 and empty contents. This marks the end of the content and tells the server it can stop parsing.

You can handle this in your code as followed:

static IResponse RequestHandler(in Request request) 
{
	if (ChunkedData.HasChunkedBody(in request))
	{
		var size = ChunkedData.GetSize(request.Body);

		// You should handle this properly!
		Debug.Assert(size > 0);

		System.Span<byte> buffer = stackalloc byte[size];

		var written = ChunkedData.Parse(request.Body, buffer);

		var body = buffer.SliceUnsafe(0, written);
	}
}

In this example, the body variable will contain all the chunk content's glued together.

However, chunked content can be encoded, just like how the server's response can be encoded ( see Xenia.Encoding for more info). You can decode the content like this (you need to install the Xenia.Encoding package for this to work!):

// ...

var encoding = ChunkedData.GetChunkEncoding(in request);

System.Span<byte> dst = stackalloc byte[128];

// `data` comes from the previous example.
var decoded = BodyEncoding.TryDecode(dst, data, encoding, out var written);

Debug.Assert(decoded);

var body = dst.SliceUnsafe(0, written);

When decoding encoded content, keep in mind that the decoded output can have a bigger size/length than the encoded input.

multipart/form-data

multipart/form-data is one of the different ways that's used to format a request's body, for example when you send a POST form.

When a request with this data type is sent, the Content-Type header will look something like this:

multipart/form-data; boundary=--BROWSER-GENERATED-NAME

The boundary here declares which parts/chunks of the request body belong to the form data. The request body contains different chunks, following this format:

--BROWSER-GENERATED-NAME
content-disposition: form-data; name="example-data"
content-type: text/plain; # This header is optional. The spec defines that if this header is missing, the Content-Type will be: text/plain

Hello world! This data has been sent using the 'multipart/form-data' format.
--BROWSER-GENERATED-NAME

You can handle this data easily in your request handler like this:

static IResponse RequestHandler(in Request request) 
{
	// Validates and parses the `Content-Type` header, so you can easily access the sent multipart data. 
	var multipart = Multipart.FromRequest(in request);

	// Make sure to check the `IsValid` boolean before trying to access the multipart data,
	// as the request may not be valid multipart data.
	if (multipart.IsValid)
	{
		var found = multipart.TryGet("username"u8, out var username);
		found = multipart.TryGet("password"u8, out var password);
		found = multipart.TryGet("file"u8, out var file);

		// Make sure to cleanup the allocated resources! 
		username.Dispose();
		password.Dispose();
		file.Dispose();
	}
}
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 166 10/29/2024