284 lines
7.6 KiB
C#
284 lines
7.6 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using System.Collections.Generic;
|
|
using Microsoft.AspNetCore.Hosting.Server;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
|
|
|
namespace xCommons.Extensions
|
|
{
|
|
/// <summary>
|
|
/// All Extensions around Http ...
|
|
/// </summary>
|
|
public static class XHttpExtensions
|
|
{
|
|
/// <summary>
|
|
/// Write Response as JSON ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="status"></param>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public static async Task WriteResponseAsJsonAsync(
|
|
this HttpContext source,
|
|
HttpStatusCode status,
|
|
object data
|
|
)
|
|
{
|
|
//
|
|
await source.Response.WriteJsonAsync(
|
|
data: data,
|
|
status: status
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write JSON ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="status"></param>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public static async Task WriteJsonAsync(
|
|
this HttpResponse source,
|
|
HttpStatusCode status,
|
|
object data
|
|
)
|
|
{
|
|
//
|
|
source.StatusCode = (int)status;
|
|
source.ContentType = "application/json; charset=utf-8";
|
|
var json = data.ToJSON();
|
|
|
|
//
|
|
await source.WriteAsync(json);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Copy Request Headers ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="dest"></param>
|
|
/// <param name="exceptions"></param>
|
|
public static void CopyRequestHeaders(
|
|
this HttpContext source,
|
|
HttpContent dest,
|
|
string[] exceptions = null
|
|
)
|
|
{
|
|
//
|
|
if (dest.IsNull() || source.IsNull())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
source.Request.CopyHeaders(
|
|
dest: dest,
|
|
exceptions: exceptions
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Copy Request Headers ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="dest"></param>
|
|
/// <param name="exceptions"></param>
|
|
public static void CopyRequestHeaders(
|
|
this HttpContext source,
|
|
HttpRequestMessage dest,
|
|
string[] exceptions = null
|
|
)
|
|
{
|
|
//
|
|
if (dest.IsNull() || source.IsNull())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
source.Request.CopyHeaders(
|
|
dest: dest,
|
|
exceptions: exceptions
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Copy Headers ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="dest"></param>
|
|
/// <param name="exceptions"></param>
|
|
public static void CopyHeaders(
|
|
this HttpRequest source,
|
|
HttpContent dest,
|
|
string[] exceptions = null
|
|
)
|
|
{
|
|
//
|
|
if (dest.IsNull() || source.IsNull())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
foreach (var header in source.Headers)
|
|
{
|
|
//
|
|
if (
|
|
exceptions.HasChild() &&
|
|
exceptions.Any(xh => xh.Equals(header.Key, StringComparison.InvariantCultureIgnoreCase))
|
|
)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
dest.Headers.TryAddWithoutValidation(
|
|
name: header.Key,
|
|
values: [.. header.Value]
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Copy Headers ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="dest"></param>
|
|
/// <param name="exceptions"></param>
|
|
public static void CopyHeaders(
|
|
this HttpRequest source,
|
|
HttpRequestMessage dest,
|
|
string[] exceptions = null
|
|
)
|
|
{
|
|
//
|
|
if (dest.IsNull() || source.IsNull())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
foreach (var header in source.Headers)
|
|
{
|
|
//
|
|
if (
|
|
exceptions.HasChild() &&
|
|
exceptions.Any(xh => xh.Equals(header.Key, StringComparison.InvariantCultureIgnoreCase))
|
|
)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
dest.Headers.TryAddWithoutValidation(
|
|
name: header.Key,
|
|
values: [.. header.Value]
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Copy Headers ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="context"></param>
|
|
/// <param name="exceptions"></param>
|
|
public static void CopyHeaders(
|
|
this HttpResponseMessage source,
|
|
HttpContext context,
|
|
string[] exceptions = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (source.IsNull() || context.IsNull())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
source.CopyHeaders(
|
|
exceptions: exceptions,
|
|
response: context.Response
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Copy Headers ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="response"></param>
|
|
/// <param name="exceptions"></param>
|
|
public static void CopyHeaders(
|
|
this HttpResponseMessage source,
|
|
HttpResponse response,
|
|
string[] exceptions = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (source.IsNull() || response.IsNull())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
foreach (var header in source.Headers)
|
|
{
|
|
//
|
|
if (
|
|
exceptions.HasChild() &&
|
|
exceptions.Any(xh => xh.Equals(header.Key, StringComparison.InvariantCultureIgnoreCase))
|
|
)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
response.Headers[header.Key] = header.Value.ToArray();
|
|
}
|
|
|
|
//
|
|
foreach (var header in source.Content.Headers)
|
|
{
|
|
//
|
|
if (
|
|
exceptions.HasChild() &&
|
|
exceptions.Any(xh => xh.Equals(header.Key, StringComparison.InvariantCultureIgnoreCase))
|
|
)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//
|
|
response.Headers[header.Key] = header.Value.ToArray();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract Application Urls ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <returns></returns>
|
|
public static ICollection<string> GetApplicationUrls(
|
|
this IServiceProvider services
|
|
)
|
|
{
|
|
//
|
|
var server = services.GetService<IServer>();
|
|
var addresses = server?.Features.Get<IServerAddressesFeature>();
|
|
var result = addresses?.Addresses ?? Array.Empty<string>();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
}
|
|
} |