WIP
This commit is contained in:
@@ -41,19 +41,39 @@ namespace VAR.WebFormsCore.Code
|
|||||||
context.Response.Body.WriteAsync(byteObject);
|
context.Response.Body.WriteAsync(byteObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void SafeSet(this IHeaderDictionary header, string key, string value)
|
||||||
|
{
|
||||||
|
if (header.ContainsKey(key))
|
||||||
|
{
|
||||||
|
header[key] = value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
header.Add(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SafeDel(this IHeaderDictionary header, string key)
|
||||||
|
{
|
||||||
|
if (header.ContainsKey(key))
|
||||||
|
{
|
||||||
|
header.Remove(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void PrepareCacheableResponse(this HttpResponse response)
|
public static void PrepareCacheableResponse(this HttpResponse response)
|
||||||
{
|
{
|
||||||
const int secondsInDay = 86400;
|
const int secondsInDay = 86400;
|
||||||
response.Headers.Add("Cache-Control", string.Format("public, max-age={0}", secondsInDay));
|
response.Headers.SafeSet("Cache-Control", string.Format("public, max-age={0}", secondsInDay));
|
||||||
string ExpireDate = DateTime.UtcNow.AddSeconds(secondsInDay).ToString("ddd, dd MMM yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
|
string ExpireDate = DateTime.UtcNow.AddSeconds(secondsInDay).ToString("ddd, dd MMM yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
|
||||||
response.Headers.Add("Expires", ExpireDate + " GMT");
|
response.Headers.SafeSet("Expires", ExpireDate + " GMT");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void PrepareUncacheableResponse(this HttpResponse response)
|
public static void PrepareUncacheableResponse(this HttpResponse response)
|
||||||
{
|
{
|
||||||
response.Headers.Add("Cache-Control", "max-age=0, no-cache, no-store");
|
response.Headers.SafeSet("Cache-Control", "max-age=0, no-cache, no-store");
|
||||||
string ExpireDate = DateTime.UtcNow.AddSeconds(-1500).ToString("ddd, dd MMM yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
|
string ExpireDate = DateTime.UtcNow.AddSeconds(-1500).ToString("ddd, dd MMM yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
|
||||||
response.Headers.Add("Expires", ExpireDate + " GMT");
|
response.Headers.SafeSet("Expires", ExpireDate + " GMT");
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion HttpContext
|
#endregion HttpContext
|
||||||
|
|||||||
@@ -24,11 +24,11 @@ namespace VAR.WebFormsCore.Code
|
|||||||
|
|
||||||
public async Task Invoke(HttpContext httpContext)
|
public async Task Invoke(HttpContext httpContext)
|
||||||
{
|
{
|
||||||
httpContext.Response.Headers.Remove("Server");
|
httpContext.Response.Headers.SafeDel("Server");
|
||||||
httpContext.Response.Headers.Remove("X-Powered-By");
|
httpContext.Response.Headers.SafeDel("X-Powered-By");
|
||||||
httpContext.Response.Headers.Add("X-Content-Type-Options", "nosniff");
|
httpContext.Response.Headers.SafeSet("X-Content-Type-Options", "nosniff");
|
||||||
httpContext.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
|
httpContext.Response.Headers.SafeSet("X-Frame-Options", "SAMEORIGIN");
|
||||||
httpContext.Response.Headers.Add("X-XSS-Protection", "1; mode=block");
|
httpContext.Response.Headers.SafeSet("X-XSS-Protection", "1; mode=block");
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ namespace VAR.WebFormsCore.Pages
|
|||||||
Render(stringWriter);
|
Render(stringWriter);
|
||||||
if (context.Response.HasStarted) { return; }
|
if (context.Response.HasStarted) { return; }
|
||||||
|
|
||||||
context.Response.Headers.Add("Content-Type", "text/html");
|
context.Response.Headers.SafeSet("Content-Type", "text/html");
|
||||||
byte[] byteObject = _utf8Econding.GetBytes(stringWriter.ToString());
|
byte[] byteObject = _utf8Econding.GetBytes(stringWriter.ToString());
|
||||||
await context.Response.Body.WriteAsync(byteObject);
|
await context.Response.Body.WriteAsync(byteObject);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<OutputType>Library</OutputType>
|
<OutputType>Library</OutputType>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user