From 526a1d295cb6e53adf20ba40336db468e94ee335 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 23 Oct 2026 19:54:51 +0330 Subject: [PATCH] Add Extension Methods for HttpContext, HttpRequest, HttpResponse and etc ... --- Extensions/XHttpExtensions.cs | 284 ++++++++++++++++++++++++++++++++++ 1 file changed, 284 insertions(+) create mode 100644 Extensions/XHttpExtensions.cs diff --git a/Extensions/XHttpExtensions.cs b/Extensions/XHttpExtensions.cs new file mode 100644 index 0000000..275aeee --- /dev/null +++ b/Extensions/XHttpExtensions.cs @@ -0,0 +1,284 @@ +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 +{ + /// + /// All Extensions around Http ... + /// + public static class XHttpExtensions + { + /// + /// Write Response as JSON ... + /// + /// + /// + /// + /// + public static async Task WriteResponseAsJsonAsync( + this HttpContext source, + HttpStatusCode status, + object data + ) + { + // + await source.Response.WriteJsonAsync( + data: data, + status: status + ); + } + + /// + /// Write JSON ... + /// + /// + /// + /// + /// + 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); + } + + /// + /// Copy Request Headers ... + /// + /// + /// + /// + 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 + ); + } + + /// + /// Copy Request Headers ... + /// + /// + /// + /// + 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 + ); + } + + /// + /// Copy Headers ... + /// + /// + /// + /// + 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] + ); + } + } + + /// + /// Copy Headers ... + /// + /// + /// + /// + 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] + ); + } + } + + /// + /// Copy Headers ... + /// + /// + /// + /// + 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 + ); + } + + /// + /// Copy Headers ... + /// + /// + /// + /// + 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(); + } + } + + /// + /// Extract Application Urls ... + /// + /// + /// + public static ICollection GetApplicationUrls( + this IServiceProvider services + ) + { + // + var server = services.GetService(); + var addresses = server?.Features.Get(); + var result = addresses?.Addresses ?? Array.Empty(); + + // + return result; + } + + } +} \ No newline at end of file