Misc:
* 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:
@@ -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, }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
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
|
||||||
|
|
||||||
|
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
|
if (header.ContainsKey(key)) { header.Remove(key); }
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion IHeaderDictionary
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
|
||||||
@@ -18,7 +27,16 @@ public class FakeWebContext : IWebContext
|
|||||||
public Dictionary<string, string?> RequestQuery { get; } = new();
|
public Dictionary<string, string?> RequestQuery { get; } = new();
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,12 +11,24 @@ public interface IWebContext
|
|||||||
Dictionary<string, string> RequestCookies { get; }
|
Dictionary<string, string> RequestCookies { get; }
|
||||||
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; }
|
||||||
|
|||||||
@@ -3,44 +3,43 @@ 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";
|
||||||
|
|
||||||
|
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 ");
|
foreach (KeyValuePair<string, string?> queryParam in Page.Context.RequestQuery)
|
||||||
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)
|
string key = ServerHelpers.UrlEncode(queryParam.Key);
|
||||||
{
|
string value = ServerHelpers.UrlEncode(queryParam.Value ?? string.Empty);
|
||||||
string key = ServerHelpers.UrlEncode(queryParam.Key);
|
sbAction.Append($"&{key}={value}");
|
||||||
string value = ServerHelpers.UrlEncode(queryParam.Value ?? string.Empty);
|
|
||||||
sbAction.Append($"&{key}={value}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return sbAction.ToString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return sbAction.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user