Skip to content

Commit

Permalink
Adding support to modular Enctype Parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed Oct 19, 2024
1 parent 3622942 commit 6a79392
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
28 changes: 24 additions & 4 deletions src/http/interfaces/IHTTP.Body.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System;
using System.Text;

namespace Netly.Interfaces
{
Expand All @@ -11,7 +12,7 @@ public interface Body
/// <br/> <i>If not found in HTTP header UTF-8 is used by Default</i>
/// </summary>
Encoding Encoding { get; }

/// <summary>
/// Enctype type
/// </summary>
Expand All @@ -28,9 +29,28 @@ public interface Body
byte[] Binary { get; }

/// <summary>
/// Enctype Parser: Make easy and seamless parse JSON, YML, UrlEncoded, and more!
/// Parse HTTP Body using Detected Enctype
/// </summary>
T Parse<T>();

/// <summary>
/// Parse HTTP Body using Custom Enctype
/// </summary>
/// <param name="enctype">Enctype Target</param>
/// <typeparam name="T">Response Object</typeparam>
/// <returns></returns>
T Parse<T>(HTTP.Enctype enctype);

/// <summary>
/// Adding Enctype parser Method
/// </summary>
EnctypeParser Parser { get; }
/// <param name="enctype">Enctype Target</param>
/// <param name="replaceOnMatch">
/// <i>true:</i> Replaces the existing <i>handler</i> with this one if both target the same <i>Enctype</i>.<br/>
/// <i>false:</i> Uses this <i>handler</i> only if no handler for the same <i>Enctype</i> is set (does not replace an existing one).
/// </param>
/// <param name="handler">Target Enctype (Enctype that handler will solve)</param>
void OnParse(HTTP.Enctype enctype, bool replaceOnMatch, Func<Type, object> handler);
}
}
}
2 changes: 1 addition & 1 deletion src/http/interfaces/IHTTP.EnctypeObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Netly.Interfaces
{
public static partial class IHTTP
{
public interface EnctypeObject
internal interface EnctypeObject
{
bool IsNull { get; }
string String { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/http/interfaces/IHTTP.EnctypeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Netly.Interfaces
{
public static partial class IHTTP
{
public interface EnctypeParser
internal interface EnctypeParser
{
bool IsValid { get; }
string[] Keys { get; }
Expand Down
45 changes: 39 additions & 6 deletions src/http/partials/HTTP.Body.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,52 @@ public partial class HTTP
{
internal class Body : IHTTP.Body
{
private readonly byte[] _binary;

private readonly Dictionary<Enctype, Func<Type, object>> _handlers;
private readonly object _parseLock = new object();
private string _text;

public Body(ref byte[] buffer, Encoding encoding, Dictionary<string, string> header)
{
Binary = buffer;
_binary = buffer;
Encoding = encoding;
Text = buffer.GetString(encoding);
_handlers = new Dictionary<Enctype, Func<Type, object>>();
Enctype = GetEnctypeFromHeader(ref header);
Parser = new EnctypeParser(Enctype, ref buffer);
}

public Enctype Enctype { get; }
public string Text { get; }
public byte[] Binary { get; }
public IHTTP.EnctypeParser Parser { get; }
byte[] IHTTP.Body.Binary => _binary;
string IHTTP.Body.Text => _text ?? (_text = _binary.GetString(Encoding));

public T Parse<T>()
{
return Parse<T>(Enctype);
}

public T Parse<T>(Enctype enctype)
{
lock (_parseLock)
{
if (_handlers.TryGetValue(enctype, out var handler))
if (handler != null)
return (T)handler(typeof(T));

return default;
}
}

public void OnParse(Enctype enctype, bool replaceOnMatch, Func<Type, object> handler)
{
lock (_parseLock)
{
if (!_handlers.ContainsKey(enctype))
_handlers.Add(enctype, handler);
else if (replaceOnMatch)
_handlers[enctype] = handler;
}
}

public Encoding Encoding { get; }

private static Enctype GetEnctypeFromHeader(ref Dictionary<string, string> headers)
Expand Down

0 comments on commit 6a79392

Please sign in to comment.