2021-01-29 10:30:28 +01:00
|
|
|
|
using System.IO;
|
2020-06-02 13:28:30 +10:00
|
|
|
|
using System.Net;
|
2020-06-15 13:07:49 +02:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2020-05-16 19:17:08 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2021-01-08 17:21:35 +11:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-06-02 13:28:30 +10:00
|
|
|
|
using Umbraco.Core;
|
2021-01-08 17:21:35 +11:00
|
|
|
|
using Umbraco.Core.Routing;
|
2020-05-16 09:41:54 +02:00
|
|
|
|
|
2020-06-02 13:28:30 +10:00
|
|
|
|
namespace Umbraco.Extensions
|
2020-05-16 09:41:54 +02:00
|
|
|
|
{
|
2021-01-08 17:21:35 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Extension methods for <see cref="HttpRequest"/>
|
|
|
|
|
|
/// </summary>
|
2020-05-16 09:41:54 +02:00
|
|
|
|
public static class HttpRequestExtensions
|
|
|
|
|
|
{
|
2020-08-07 00:48:32 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Check if a preview cookie exist
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static bool HasPreviewCookie(this HttpRequest request)
|
2020-12-14 17:43:00 +11:00
|
|
|
|
=> request.Cookies.TryGetValue(Constants.Web.PreviewCookieName, out var cookieVal) && !cookieVal.IsNullOrWhiteSpace();
|
2020-08-07 00:48:32 +10:00
|
|
|
|
|
2021-01-08 17:21:35 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns true if the request is a back office request
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static bool IsBackOfficeRequest(this HttpRequest request)
|
|
|
|
|
|
{
|
|
|
|
|
|
PathString absPath = request.Path;
|
|
|
|
|
|
UmbracoRequestPaths umbReqPaths = request.HttpContext.RequestServices.GetService<UmbracoRequestPaths>();
|
|
|
|
|
|
return umbReqPaths.IsBackOfficeRequest(absPath);
|
|
|
|
|
|
}
|
2020-06-02 13:28:30 +10:00
|
|
|
|
|
2021-01-08 17:21:35 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns true if the request is for a client side extension
|
|
|
|
|
|
/// </summary>
|
2020-06-02 13:28:30 +10:00
|
|
|
|
public static bool IsClientSideRequest(this HttpRequest request)
|
2021-01-08 17:21:35 +11:00
|
|
|
|
{
|
|
|
|
|
|
PathString absPath = request.Path;
|
|
|
|
|
|
UmbracoRequestPaths umbReqPaths = request.HttpContext.RequestServices.GetService<UmbracoRequestPaths>();
|
|
|
|
|
|
return umbReqPaths.IsClientSideRequest(absPath);
|
|
|
|
|
|
}
|
2020-06-02 13:28:30 +10:00
|
|
|
|
|
2020-06-15 13:07:49 +02:00
|
|
|
|
public static string ClientCulture(this HttpRequest request)
|
2020-12-14 17:43:00 +11:00
|
|
|
|
=> request.Headers.TryGetValue("X-UMB-CULTURE", out var values) ? values[0] : null;
|
2020-05-16 19:17:08 +02:00
|
|
|
|
|
|
|
|
|
|
/// <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())
|
|
|
|
|
|
{
|
|
|
|
|
|
// We have a remote address set up
|
|
|
|
|
|
return connection.LocalIpAddress.IsSet()
|
|
|
|
|
|
// 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;
|
2020-06-15 13:07:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetRawBodyString(this HttpRequest request, Encoding encoding = null)
|
|
|
|
|
|
{
|
2021-01-29 10:30:28 +01:00
|
|
|
|
if (request.Body.CanSeek)
|
|
|
|
|
|
{
|
|
|
|
|
|
request.Body.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
}
|
2020-06-15 13:07:49 +02:00
|
|
|
|
|
2020-07-07 14:00:47 +02:00
|
|
|
|
using (var reader = new StreamReader(request.Body, encoding ?? Encoding.UTF8, leaveOpen: true))
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = reader.ReadToEnd();
|
2021-01-29 10:30:28 +01:00
|
|
|
|
if (request.Body.CanSeek)
|
|
|
|
|
|
{
|
|
|
|
|
|
request.Body.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
}
|
2021-02-01 11:26:17 +01:00
|
|
|
|
|
2020-07-07 14:00:47 +02:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2020-06-26 08:20:36 +02:00
|
|
|
|
}
|
2020-06-15 13:07:49 +02:00
|
|
|
|
|
2020-06-26 08:20:36 +02:00
|
|
|
|
public static async Task<string> GetRawBodyStringAsync(this HttpRequest request, Encoding encoding = null)
|
|
|
|
|
|
{
|
2021-01-29 10:30:28 +01:00
|
|
|
|
if (!request.Body.CanSeek)
|
|
|
|
|
|
{
|
|
|
|
|
|
request.EnableBuffering();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-26 08:20:36 +02:00
|
|
|
|
request.Body.Seek(0, SeekOrigin.Begin);
|
2020-06-15 13:07:49 +02:00
|
|
|
|
|
2020-07-07 14:00:47 +02:00
|
|
|
|
using (var reader = new StreamReader(request.Body, encoding ?? Encoding.UTF8, leaveOpen: true))
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await reader.ReadToEndAsync();
|
|
|
|
|
|
request.Body.Seek(0, SeekOrigin.Begin);
|
2021-01-29 10:30:28 +01:00
|
|
|
|
|
2020-07-07 14:00:47 +02:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2020-05-16 19:17:08 +02:00
|
|
|
|
}
|
2020-05-16 09:41:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|