* IWebContext: Add Request body reading methods and properties.
* IWebContext: Add support for HttpOnly, and Secure cookies.
* Use file-scoped namespaces.
This commit is contained in:
2025-07-28 16:11:54 +02:00
parent 6dc19e8bbd
commit 9a480f5528
5 changed files with 110 additions and 49 deletions

View File

@@ -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 ResponseWrite(string text) { _context.Response.WriteAsync(text).GetAwaiter().GetResult(); }
public void ResponseWriteBin(byte[] content) public void ResponseWriteBin(byte[] content)
@@ -97,12 +118,18 @@ public class AspnetCoreWebContext : IWebContext
public void ResponseRedirect(string url) { _context.Response.Redirect(url); } 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( _context.Response.Cookies.Append(
key: cookieName, key: cookieName,
value: value, value: value,
options: new CookieOptions { Expires = expiration, } options: new CookieOptions { Expires = expiration, HttpOnly = httpOnly, Secure = secure, }
); );
} }

View File

@@ -1,9 +1,9 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
namespace VAR.WebFormsCore.AspNetCore.Code namespace VAR.WebFormsCore.AspNetCore.Code;
public static class ExtensionMethods
{ {
public static class ExtensionMethods
{
#region IHeaderDictionary #region IHeaderDictionary
public static void SafeSet(this IHeaderDictionary header, string key, string value) { header[key] = value; } public static void SafeSet(this IHeaderDictionary header, string key, string value) { header[key] = value; }
@@ -14,5 +14,4 @@ namespace VAR.WebFormsCore.AspNetCore.Code
} }
#endregion IHeaderDictionary #endregion IHeaderDictionary
}
} }

View File

@@ -5,7 +5,16 @@ namespace VAR.WebFormsCore.Tests.Fakes;
public class FakeWebContext : IWebContext 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; public string RequestPath => string.Empty;
@@ -19,6 +28,15 @@ public class FakeWebContext : IWebContext
public Dictionary<string, string?> RequestForm { get; } = new(); public Dictionary<string, string?> RequestForm { get; } = new();
public string? RequestContentType { get; }
public long? RequestContentLength { get; }
public byte[]? RequestReadBin()
{
throw new NotImplementedException();
}
public List<WritePackage> FakeWritePackages { get; } = new(); public List<WritePackage> FakeWritePackages { get; } = new();
public void ResponseWrite(string text) { FakeWritePackages.Add(new WritePackage { Text = text, }); } public void ResponseWrite(string text) { FakeWritePackages.Add(new WritePackage { Text = text, }); }
@@ -36,7 +54,13 @@ public class FakeWebContext : IWebContext
SetResponseHeader("location", url); 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(); throw new NotImplementedException();
} }

View File

@@ -12,11 +12,23 @@ public interface IWebContext
Dictionary<string, string?> RequestQuery { get; } Dictionary<string, string?> RequestQuery { get; }
Dictionary<string, string?> RequestForm { get; } Dictionary<string, string?> RequestForm { get; }
string? RequestContentType { get; }
long? RequestContentLength { get; }
byte[]? RequestReadBin();
void ResponseWrite(string text); void ResponseWrite(string text);
void ResponseWriteBin(byte[] content); void ResponseWriteBin(byte[] content);
void ResponseFlush(); void ResponseFlush();
void ResponseRedirect(string url); 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); void DelResponseCookie(string cookieName);
bool ResponseHasStarted { get; } bool ResponseHasStarted { get; }

View File

@@ -3,10 +3,10 @@ using System.IO;
using System.Text; using System.Text;
using VAR.WebFormsCore.Code; 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"; private readonly string _method = "post";
protected override void Render(TextWriter textWriter) protected override void Render(TextWriter textWriter)
@@ -42,5 +42,4 @@ namespace VAR.WebFormsCore.Controls
return sbAction.ToString(); return sbAction.ToString();
} }
}
} }