v10: Fix build warnings in Web.Common (#12349)
* Run code cleanup * Run dotnet format * Start manual cleanup in Web.Common * Finish up manual cleanup * Fix tests * Fix up InMemoryModelFactory.cs * Inject proper macroRenderer * Update src/Umbraco.Web.Common/Filters/JsonDateTimeFormatAttribute.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Web.Common/Filters/ValidateUmbracoFormRouteStringAttribute.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Fix based on review Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk> Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
This commit is contained in:
@@ -1,161 +1,162 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Extensions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Configuration.Models;
|
||||
using Umbraco.Cms.Core.Routing;
|
||||
|
||||
namespace Umbraco.Extensions
|
||||
namespace Umbraco.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="HttpRequest" />
|
||||
/// </summary>
|
||||
public static class HttpRequestExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="HttpRequest"/>
|
||||
/// Check if a preview cookie exist
|
||||
/// </summary>
|
||||
public static class HttpRequestExtensions
|
||||
public static bool HasPreviewCookie(this HttpRequest request)
|
||||
=> request.Cookies.TryGetValue(Constants.Web.PreviewCookieName, out var cookieVal) &&
|
||||
!cookieVal.IsNullOrWhiteSpace();
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the request is a back office request
|
||||
/// </summary>
|
||||
public static bool IsBackOfficeRequest(this HttpRequest request)
|
||||
{
|
||||
/// <summary>
|
||||
/// Check if a preview cookie exist
|
||||
/// </summary>
|
||||
public static bool HasPreviewCookie(this HttpRequest request)
|
||||
=> request.Cookies.TryGetValue(Cms.Core.Constants.Web.PreviewCookieName, out var cookieVal) && !cookieVal.IsNullOrWhiteSpace();
|
||||
PathString absPath = request.Path;
|
||||
UmbracoRequestPaths? umbReqPaths = request.HttpContext.RequestServices.GetService<UmbracoRequestPaths>();
|
||||
return umbReqPaths?.IsBackOfficeRequest(absPath) ?? false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the request is a back office request
|
||||
/// </summary>
|
||||
public static bool IsBackOfficeRequest(this HttpRequest request)
|
||||
/// <summary>
|
||||
/// Returns true if the request is for a client side extension
|
||||
/// </summary>
|
||||
public static bool IsClientSideRequest(this HttpRequest request)
|
||||
{
|
||||
PathString absPath = request.Path;
|
||||
UmbracoRequestPaths? umbReqPaths = request.HttpContext.RequestServices.GetService<UmbracoRequestPaths>();
|
||||
return umbReqPaths?.IsClientSideRequest(absPath) ?? false;
|
||||
}
|
||||
|
||||
public static string? ClientCulture(this HttpRequest request)
|
||||
=> request.Headers.TryGetValue("X-UMB-CULTURE", out StringValues values) ? values[0] : null;
|
||||
|
||||
/// <summary>
|
||||
/// Determines if a request is local.
|
||||
/// </summary>
|
||||
/// <returns>True if request is local</returns>
|
||||
/// <remarks>
|
||||
/// Hat-tip: https://stackoverflow.com/a/41242493/489433
|
||||
/// </remarks>
|
||||
public static bool IsLocal(this HttpRequest request)
|
||||
{
|
||||
ConnectionInfo connection = request.HttpContext.Connection;
|
||||
if (connection.RemoteIpAddress?.IsSet() ?? false)
|
||||
{
|
||||
PathString absPath = request.Path;
|
||||
UmbracoRequestPaths? umbReqPaths = request.HttpContext.RequestServices.GetService<UmbracoRequestPaths>();
|
||||
return umbReqPaths?.IsBackOfficeRequest(absPath) ?? false;
|
||||
// We have a remote address set up
|
||||
return connection.LocalIpAddress?.IsSet() ?? false
|
||||
|
||||
// Is local is same as remote, then we are local
|
||||
? connection.RemoteIpAddress.Equals(connection.LocalIpAddress)
|
||||
|
||||
// else we are remote if the remote IP address is not a loopback address
|
||||
: IPAddress.IsLoopback(connection.RemoteIpAddress);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the request is for a client side extension
|
||||
/// </summary>
|
||||
public static bool IsClientSideRequest(this HttpRequest request)
|
||||
return true;
|
||||
}
|
||||
|
||||
public static string GetRawBodyString(this HttpRequest request, Encoding? encoding = null)
|
||||
{
|
||||
if (request.Body.CanSeek)
|
||||
{
|
||||
PathString absPath = request.Path;
|
||||
UmbracoRequestPaths? umbReqPaths = request.HttpContext.RequestServices.GetService<UmbracoRequestPaths>();
|
||||
return umbReqPaths?.IsClientSideRequest(absPath) ?? false;
|
||||
request.Body.Seek(0, SeekOrigin.Begin);
|
||||
}
|
||||
|
||||
public static string? ClientCulture(this HttpRequest request)
|
||||
=> request.Headers.TryGetValue("X-UMB-CULTURE", out var values) ? values[0] : null;
|
||||
|
||||
/// <summary>
|
||||
/// Determines if a request is local.
|
||||
/// </summary>
|
||||
/// <returns>True if request is local</returns>
|
||||
/// <remarks>
|
||||
/// Hat-tip: https://stackoverflow.com/a/41242493/489433
|
||||
/// </remarks>
|
||||
public static bool IsLocal(this HttpRequest request)
|
||||
{
|
||||
var connection = request.HttpContext.Connection;
|
||||
if (connection.RemoteIpAddress?.IsSet() ?? false)
|
||||
{
|
||||
// We have a remote address set up
|
||||
return connection.LocalIpAddress?.IsSet() ?? false
|
||||
// Is local is same as remote, then we are local
|
||||
? connection.RemoteIpAddress.Equals(connection.LocalIpAddress)
|
||||
// else we are remote if the remote IP address is not a loopback address
|
||||
: IPAddress.IsLoopback(connection.RemoteIpAddress);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsSet(this IPAddress address)
|
||||
{
|
||||
const string NullIpAddress = "::1";
|
||||
return address != null && address.ToString() != NullIpAddress;
|
||||
}
|
||||
|
||||
public static string GetRawBodyString(this HttpRequest request, Encoding? encoding = null)
|
||||
using (var reader = new StreamReader(request.Body, encoding ?? Encoding.UTF8, leaveOpen: true))
|
||||
{
|
||||
var result = reader.ReadToEnd();
|
||||
if (request.Body.CanSeek)
|
||||
{
|
||||
request.Body.Seek(0, SeekOrigin.Begin);
|
||||
}
|
||||
|
||||
using (var reader = new StreamReader(request.Body, encoding ?? Encoding.UTF8, leaveOpen: true))
|
||||
{
|
||||
var result = reader.ReadToEnd();
|
||||
if (request.Body.CanSeek)
|
||||
{
|
||||
request.Body.Seek(0, SeekOrigin.Begin);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<string> GetRawBodyStringAsync(this HttpRequest request, Encoding? encoding = null)
|
||||
{
|
||||
if (!request.Body.CanSeek)
|
||||
{
|
||||
request.EnableBuffering();
|
||||
}
|
||||
|
||||
request.Body.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
using (var reader = new StreamReader(request.Body, encoding ?? Encoding.UTF8, leaveOpen: true))
|
||||
{
|
||||
var result = await reader.ReadToEndAsync();
|
||||
request.Body.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the application URI, will use the one specified in settings if present
|
||||
/// </summary>
|
||||
public static Uri GetApplicationUri(this HttpRequest request, WebRoutingSettings routingSettings)
|
||||
{
|
||||
if (request == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(request));
|
||||
}
|
||||
|
||||
if (routingSettings == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(routingSettings));
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(routingSettings.UmbracoApplicationUrl))
|
||||
{
|
||||
var requestUri = new Uri(request.GetDisplayUrl());
|
||||
|
||||
// Create a new URI with the relative uri as /, this ensures that only the base path is returned.
|
||||
return new Uri(requestUri, "/");
|
||||
}
|
||||
|
||||
return new Uri(routingSettings.UmbracoApplicationUrl);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Umbraco `ufprt` encrypted string from the current request
|
||||
/// </summary>
|
||||
/// <param name="request">The current request</param>
|
||||
/// <returns>The extracted `ufprt` token.</returns>
|
||||
public static string? GetUfprt(this HttpRequest request)
|
||||
{
|
||||
if (request.HasFormContentType && request.Form.TryGetValue("ufprt", out StringValues formVal) && formVal != StringValues.Empty)
|
||||
{
|
||||
return formVal.ToString();
|
||||
}
|
||||
|
||||
if (request.Query.TryGetValue("ufprt", out StringValues queryVal) && queryVal != StringValues.Empty)
|
||||
{
|
||||
return queryVal.ToString();
|
||||
}
|
||||
|
||||
return null;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<string> GetRawBodyStringAsync(this HttpRequest request, Encoding? encoding = null)
|
||||
{
|
||||
if (!request.Body.CanSeek)
|
||||
{
|
||||
request.EnableBuffering();
|
||||
}
|
||||
|
||||
request.Body.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
using (var reader = new StreamReader(request.Body, encoding ?? Encoding.UTF8, leaveOpen: true))
|
||||
{
|
||||
var result = await reader.ReadToEndAsync();
|
||||
request.Body.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the application URI, will use the one specified in settings if present
|
||||
/// </summary>
|
||||
public static Uri GetApplicationUri(this HttpRequest request, WebRoutingSettings routingSettings)
|
||||
{
|
||||
if (request == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(request));
|
||||
}
|
||||
|
||||
if (routingSettings == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(routingSettings));
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(routingSettings.UmbracoApplicationUrl))
|
||||
{
|
||||
var requestUri = new Uri(request.GetDisplayUrl());
|
||||
|
||||
// Create a new URI with the relative uri as /, this ensures that only the base path is returned.
|
||||
return new Uri(requestUri, "/");
|
||||
}
|
||||
|
||||
return new Uri(routingSettings.UmbracoApplicationUrl);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Umbraco `ufprt` encrypted string from the current request
|
||||
/// </summary>
|
||||
/// <param name="request">The current request</param>
|
||||
/// <returns>The extracted `ufprt` token.</returns>
|
||||
public static string? GetUfprt(this HttpRequest request)
|
||||
{
|
||||
if (request.HasFormContentType && request.Form.TryGetValue("ufprt", out StringValues formVal) &&
|
||||
formVal != StringValues.Empty)
|
||||
{
|
||||
return formVal.ToString();
|
||||
}
|
||||
|
||||
if (request.Query.TryGetValue("ufprt", out StringValues queryVal) && queryVal != StringValues.Empty)
|
||||
{
|
||||
return queryVal.ToString();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static bool IsSet(this IPAddress address)
|
||||
{
|
||||
const string nullIpAddress = "::1";
|
||||
return address.ToString() != nullIpAddress;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user