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; } } }