* 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 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, }
);
}

View File

@@ -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
}

View File

@@ -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<string, string?> RequestQuery { 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 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();
}

View File

@@ -11,12 +11,24 @@ public interface IWebContext
Dictionary<string, string> RequestCookies { get; }
Dictionary<string, string?> RequestQuery { get; }
Dictionary<string, string?> 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; }

View File

@@ -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("<form ");
RenderAttributes(textWriter);
RenderAttribute(textWriter, "method", _method);
RenderAttribute(textWriter, "action", GetAction());
textWriter.Write(">");
protected override void Render(TextWriter textWriter)
base.Render(textWriter);
textWriter.Write("</form>");
}
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("<form ");
RenderAttributes(textWriter);
RenderAttribute(textWriter, "method", _method);
RenderAttribute(textWriter, "action", GetAction());
textWriter.Write(">");
base.Render(textWriter);
textWriter.Write("</form>");
}
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<string, string?> queryParam in Page.Context.RequestQuery)
{
foreach (KeyValuePair<string, string?> 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();
}
}