diff --git a/src/http/interfaces/IHTTP.Body.cs b/src/http/interfaces/IHTTP.Body.cs index 12c6e52..321d633 100644 --- a/src/http/interfaces/IHTTP.Body.cs +++ b/src/http/interfaces/IHTTP.Body.cs @@ -1,4 +1,5 @@ -using System.Text; +using System; +using System.Text; namespace Netly.Interfaces { @@ -11,7 +12,7 @@ public interface Body ///
If not found in HTTP header UTF-8 is used by Default /// Encoding Encoding { get; } - + /// /// Enctype type /// @@ -28,9 +29,28 @@ public interface Body byte[] Binary { get; } /// - /// Enctype Parser: Make easy and seamless parse JSON, YML, UrlEncoded, and more! + /// Parse HTTP Body using Detected Enctype + /// + T Parse(); + + /// + /// Parse HTTP Body using Custom Enctype + /// + /// Enctype Target + /// Response Object + /// + T Parse(HTTP.Enctype enctype); + + /// + /// Adding Enctype parser Method /// - EnctypeParser Parser { get; } + /// Enctype Target + /// + /// true: Replaces the existing handler with this one if both target the same Enctype.
+ /// false: Uses this handler only if no handler for the same Enctype is set (does not replace an existing one). + /// + /// Target Enctype (Enctype that handler will solve) + void OnParse(HTTP.Enctype enctype, bool replaceOnMatch, Func handler); } } } \ No newline at end of file diff --git a/src/http/interfaces/IHTTP.EnctypeObject.cs b/src/http/interfaces/IHTTP.EnctypeObject.cs index a5b3f65..5a1b79f 100644 --- a/src/http/interfaces/IHTTP.EnctypeObject.cs +++ b/src/http/interfaces/IHTTP.EnctypeObject.cs @@ -2,7 +2,7 @@ namespace Netly.Interfaces { public static partial class IHTTP { - public interface EnctypeObject + internal interface EnctypeObject { bool IsNull { get; } string String { get; } diff --git a/src/http/interfaces/IHTTP.EnctypeParser.cs b/src/http/interfaces/IHTTP.EnctypeParser.cs index 9f3f5be..49b2ebd 100644 --- a/src/http/interfaces/IHTTP.EnctypeParser.cs +++ b/src/http/interfaces/IHTTP.EnctypeParser.cs @@ -2,7 +2,7 @@ namespace Netly.Interfaces { public static partial class IHTTP { - public interface EnctypeParser + internal interface EnctypeParser { bool IsValid { get; } string[] Keys { get; } diff --git a/src/http/partials/HTTP.Body.cs b/src/http/partials/HTTP.Body.cs index aa82e3e..42561e3 100644 --- a/src/http/partials/HTTP.Body.cs +++ b/src/http/partials/HTTP.Body.cs @@ -11,19 +11,52 @@ public partial class HTTP { internal class Body : IHTTP.Body { + private readonly byte[] _binary; + + private readonly Dictionary> _handlers; + private readonly object _parseLock = new object(); + private string _text; + public Body(ref byte[] buffer, Encoding encoding, Dictionary header) { - Binary = buffer; + _binary = buffer; Encoding = encoding; - Text = buffer.GetString(encoding); + _handlers = new Dictionary>(); 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() + { + return Parse(Enctype); + } + + public T Parse(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 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 headers)