diff --git a/VAR.WebFormsCore.AspNetCore/Code/AspnetCoreWebContext.cs b/VAR.WebFormsCore.AspNetCore/Code/AspnetCoreWebContext.cs index 35cc333..ee2556c 100644 --- a/VAR.WebFormsCore.AspNetCore/Code/AspnetCoreWebContext.cs +++ b/VAR.WebFormsCore.AspNetCore/Code/AspnetCoreWebContext.cs @@ -86,6 +86,27 @@ public class AspnetCoreWebContext : IWebContext } } + public string? RequestContentType => _context.Request.ContentType; + + public long? RequestContentLength => _context.Request.ContentLength; + + public byte[]? RequestReadBin() + { + if ((_context.Request.ContentLength ?? 0) == 0) { return null; } + + byte[] content = new byte[_context.Request.ContentLength??0]; + int pendingRead = (int) (_context.Request.ContentLength ?? 0); + int currentOffset = 0; + while (pendingRead > 0) + { + int readCount = _context.Request.Body.ReadAsync(content, currentOffset, pendingRead).GetAwaiter().GetResult(); + currentOffset += readCount; + pendingRead -= readCount; + } + + return content; + } + public void ResponseWrite(string text) { _context.Response.WriteAsync(text).GetAwaiter().GetResult(); } public void ResponseWriteBin(byte[] content) @@ -97,12 +118,18 @@ public class AspnetCoreWebContext : IWebContext public void ResponseRedirect(string url) { _context.Response.Redirect(url); } - public void AddResponseCookie(string cookieName, string value, DateTime? expiration = null) + public void AddResponseCookie( + string cookieName, + string value, + DateTime? expiration = null, + bool httpOnly = false, + bool secure = false + ) { _context.Response.Cookies.Append( key: cookieName, value: value, - options: new CookieOptions { Expires = expiration, } + options: new CookieOptions { Expires = expiration, HttpOnly = httpOnly, Secure = secure, } ); } diff --git a/VAR.WebFormsCore.AspNetCore/Code/ExtensionMethods.cs b/VAR.WebFormsCore.AspNetCore/Code/ExtensionMethods.cs index 0c57941..b8154f1 100644 --- a/VAR.WebFormsCore.AspNetCore/Code/ExtensionMethods.cs +++ b/VAR.WebFormsCore.AspNetCore/Code/ExtensionMethods.cs @@ -1,18 +1,17 @@ using Microsoft.AspNetCore.Http; -namespace VAR.WebFormsCore.AspNetCore.Code +namespace VAR.WebFormsCore.AspNetCore.Code; + +public static class ExtensionMethods { - public static class ExtensionMethods + #region IHeaderDictionary + + public static void SafeSet(this IHeaderDictionary header, string key, string value) { header[key] = value; } + + public static void SafeDel(this IHeaderDictionary header, string key) { - #region IHeaderDictionary - - public static void SafeSet(this IHeaderDictionary header, string key, string value) { header[key] = value; } - - public static void SafeDel(this IHeaderDictionary header, string key) - { - if (header.ContainsKey(key)) { header.Remove(key); } - } - - #endregion IHeaderDictionary + if (header.ContainsKey(key)) { header.Remove(key); } } + + #endregion IHeaderDictionary } \ No newline at end of file diff --git a/VAR.WebFormsCore.Tests/Fakes/FakeWebContext.cs b/VAR.WebFormsCore.Tests/Fakes/FakeWebContext.cs index f58d9eb..3621582 100644 --- a/VAR.WebFormsCore.Tests/Fakes/FakeWebContext.cs +++ b/VAR.WebFormsCore.Tests/Fakes/FakeWebContext.cs @@ -5,7 +5,16 @@ namespace VAR.WebFormsCore.Tests.Fakes; public class FakeWebContext : IWebContext { - public FakeWebContext(string requestMethod = "GET") { RequestMethod = requestMethod; } + public FakeWebContext( + string requestMethod = "GET", + string? requestContentType = null, + long? requestContentLength = null + ) + { + RequestMethod = requestMethod; + RequestContentType = requestContentType; + RequestContentLength = requestContentLength; + } public string RequestPath => string.Empty; @@ -18,7 +27,16 @@ public class FakeWebContext : IWebContext public Dictionary RequestQuery { get; } = new(); public Dictionary RequestForm { get; } = new(); + + public string? RequestContentType { get; } + + public long? RequestContentLength { get; } + public byte[]? RequestReadBin() + { + throw new NotImplementedException(); + } + public List FakeWritePackages { get; } = new(); public void ResponseWrite(string text) { FakeWritePackages.Add(new WritePackage { Text = text, }); } @@ -36,7 +54,13 @@ public class FakeWebContext : IWebContext SetResponseHeader("location", url); } - public void AddResponseCookie(string cookieName, string value, DateTime? expiration = null) + public void AddResponseCookie( + string cookieName, + string value, + DateTime? expiration = null, + bool httpOnly = false, + bool secure = false + ) { throw new NotImplementedException(); } diff --git a/VAR.WebFormsCore/Code/IWebContext.cs b/VAR.WebFormsCore/Code/IWebContext.cs index dddfea8..ccf33bf 100644 --- a/VAR.WebFormsCore/Code/IWebContext.cs +++ b/VAR.WebFormsCore/Code/IWebContext.cs @@ -11,12 +11,24 @@ public interface IWebContext Dictionary RequestCookies { get; } Dictionary RequestQuery { get; } Dictionary RequestForm { get; } + + string? RequestContentType { get; } + long? RequestContentLength { get; } + byte[]? RequestReadBin(); void ResponseWrite(string text); void ResponseWriteBin(byte[] content); void ResponseFlush(); void ResponseRedirect(string url); - void AddResponseCookie(string cookieName, string value, DateTime? expiration = null); + + void AddResponseCookie( + string cookieName, + string value, + DateTime? expiration = null, + bool httpOnly = false, + bool secure = false + ); + void DelResponseCookie(string cookieName); bool ResponseHasStarted { get; } diff --git a/VAR.WebFormsCore/Controls/HtmlForm.cs b/VAR.WebFormsCore/Controls/HtmlForm.cs index eff8053..608f19b 100644 --- a/VAR.WebFormsCore/Controls/HtmlForm.cs +++ b/VAR.WebFormsCore/Controls/HtmlForm.cs @@ -3,44 +3,43 @@ using System.IO; using System.Text; using VAR.WebFormsCore.Code; -namespace VAR.WebFormsCore.Controls +namespace VAR.WebFormsCore.Controls; + +public class HtmlForm : Control { - public class HtmlForm : Control + private readonly string _method = "post"; + + protected override void Render(TextWriter textWriter) { - private readonly string _method = "post"; + textWriter.Write("
"); - protected override void Render(TextWriter textWriter) + base.Render(textWriter); + + textWriter.Write("
"); + } + + private string GetAction() + { + StringBuilder sbAction = new(); + sbAction.Append(Page?.GetType().Name); + + if ((Page?.Context?.RequestQuery.Count ?? 0) <= 0) { return sbAction.ToString(); } + + sbAction.Append('?'); + if (Page?.Context?.RequestQuery != null) { - textWriter.Write("
"); - - base.Render(textWriter); - - textWriter.Write("
"); - } - - private string GetAction() - { - StringBuilder sbAction = new(); - sbAction.Append(Page?.GetType().Name); - - if ((Page?.Context?.RequestQuery.Count ?? 0) <= 0) { return sbAction.ToString(); } - - sbAction.Append('?'); - if (Page?.Context?.RequestQuery != null) + foreach (KeyValuePair queryParam in Page.Context.RequestQuery) { - foreach (KeyValuePair queryParam in Page.Context.RequestQuery) - { - string key = ServerHelpers.UrlEncode(queryParam.Key); - string value = ServerHelpers.UrlEncode(queryParam.Value ?? string.Empty); - sbAction.Append($"&{key}={value}"); - } + string key = ServerHelpers.UrlEncode(queryParam.Key); + string value = ServerHelpers.UrlEncode(queryParam.Value ?? string.Empty); + sbAction.Append($"&{key}={value}"); } - - return sbAction.ToString(); } + + return sbAction.ToString(); } } \ No newline at end of file