generated from OoLunar/DSharpPlus.Template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement HyperSerializerDelegate for easy expansion on content-types
- Loading branch information
Showing
10 changed files
with
114 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace HyperSharp.Protocol | ||
{ | ||
/// <summary> | ||
/// The delegate to be called when serializing the body to the client. | ||
/// </summary> | ||
/// <param name="context">The <see cref="HyperContext"/> for the current request.</param> | ||
/// <param name="status">The returned <see cref="HyperStatus"/> from the responder.</param> | ||
/// <param name="cancellationToken">A cancellation token to stop the serializer.</param> | ||
/// <returns>A <see cref="ValueTask"/> representing the asynchronous operation, returning a <see cref="bool"/> indicating whether the serializer was successful.</returns> | ||
public delegate ValueTask<bool> HyperSerializerDelegate(HyperContext context, HyperStatus status, CancellationToken cancellationToken = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Toolkit.HighPerformance; | ||
|
||
namespace HyperSharp.Protocol | ||
{ | ||
public static partial class HyperSerializers | ||
{ | ||
private static readonly byte[] _jsonEncodingHeader = "Content-Type: application/json; charset=utf-8\r\nContent-Length: "u8.ToArray(); | ||
|
||
/// <summary> | ||
/// Serializes the body to the client as JSON using the <see cref="JsonSerializer.SerializeToUtf8Bytes{TValue}(TValue, JsonSerializerOptions?)"/> method with the <see cref="HyperConfiguration.JsonSerializerOptions"/> options. | ||
/// </summary> | ||
/// <remarks> | ||
/// This serializer is the default serializer for HyperSharp. | ||
/// </remarks> | ||
/// <inheritdoc cref="HyperSerializerDelegate"/> | ||
public static ValueTask<bool> JsonAsync(HyperContext context, HyperStatus status, CancellationToken cancellationToken = default) | ||
{ | ||
ArgumentNullException.ThrowIfNull(context); | ||
ArgumentNullException.ThrowIfNull(status); | ||
|
||
// Write Content-Type header and beginning of Content-Length header | ||
context.Connection.StreamWriter.Write<byte>(_jsonEncodingHeader); | ||
byte[] body = JsonSerializer.SerializeToUtf8Bytes(status.Body, context.Connection.Server.Configuration.JsonSerializerOptions); | ||
|
||
// Finish the Content-Length header | ||
context.Connection.StreamWriter.Write<byte>(Encoding.ASCII.GetBytes(body.Length.ToString())); // TODO: This could probably be done without allocating a string | ||
context.Connection.StreamWriter.Write<byte>(_newLine); | ||
|
||
// Write body | ||
context.Connection.StreamWriter.Write<byte>(_newLine); | ||
context.Connection.StreamWriter.Write<byte>(body); | ||
|
||
return ValueTask.FromResult(true); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/HyperSharp/Protocol/HyperSerializers/PlainTextAsync.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Toolkit.HighPerformance; | ||
|
||
namespace HyperSharp.Protocol | ||
{ | ||
/// <summary> | ||
/// Holds a collection of static methods implementing <see cref="HyperSerializerDelegate"/> for the most common of Content-Types. | ||
/// </summary> | ||
public static partial class HyperSerializers | ||
{ | ||
private static readonly byte[] _newLine = "\r\n"u8.ToArray(); | ||
private static readonly byte[] _contentTypeJsonEncodingHeader = "Content-Type: application/json; charset=utf-8\r\nContent-Length: "u8.ToArray(); | ||
|
||
/// <summary> | ||
/// Serializes the body to the client as plain text using the <see cref="object.ToString"/> method with the <see cref="Encoding.UTF8"/> encoding. | ||
/// </summary> | ||
/// <inheritdoc cref="HyperSerializerDelegate"/> | ||
public static ValueTask<bool> PlainTextAsync(HyperContext context, HyperStatus status, CancellationToken cancellationToken = default) | ||
{ | ||
ArgumentNullException.ThrowIfNull(context); | ||
ArgumentNullException.ThrowIfNull(status); | ||
|
||
// Write Content-Type header and beginning of Content-Length header | ||
context.Connection.StreamWriter.Write<byte>(_contentTypeJsonEncodingHeader); | ||
|
||
byte[] body = Encoding.UTF8.GetBytes(status.Body?.ToString() ?? ""); | ||
|
||
// Write Content-Length header | ||
context.Connection.StreamWriter.Write<byte>(Encoding.ASCII.GetBytes(body.Length.ToString())); // TODO: This could probably be done without allocating a string | ||
context.Connection.StreamWriter.Write<byte>(_newLine); | ||
|
||
// Write body | ||
context.Connection.StreamWriter.Write<byte>(_newLine); | ||
context.Connection.StreamWriter.Write<byte>(body); | ||
|
||
return ValueTask.FromResult(true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters