add http request hook
This commit is contained in:
parent
b681b18ac2
commit
3b63b8b720
|
|
@ -0,0 +1,8 @@
|
|||
using System.Net.Http.Headers;
|
||||
|
||||
namespace BotSharp.Abstraction.Http;
|
||||
|
||||
public interface IHttpRequestHook
|
||||
{
|
||||
void OnAddHttpHeaders(HttpHeaders headers);
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
using BotSharp.Abstraction.Users.Models;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace BotSharp.Abstraction.Users;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System.Net.Http;
|
||||
using System.Net.Mime;
|
||||
using BotSharp.Abstraction.Http;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
|
@ -13,7 +14,6 @@ public class HandleHttpRequestFn : IFunctionCallback
|
|||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger<HandleHttpRequestFn> _logger;
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IHttpContextAccessor _context;
|
||||
private readonly BotSharpOptions _options;
|
||||
|
||||
public HandleHttpRequestFn(IServiceProvider services,
|
||||
|
|
@ -25,7 +25,6 @@ public class HandleHttpRequestFn : IFunctionCallback
|
|||
_services = services;
|
||||
_logger = logger;
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_context = context;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
|
|
@ -57,7 +56,7 @@ public class HandleHttpRequestFn : IFunctionCallback
|
|||
if (string.IsNullOrEmpty(url)) return null;
|
||||
|
||||
using var client = _httpClientFactory.CreateClient();
|
||||
AddRequestHeaders(client);
|
||||
PrepareRequestHeaders(client);
|
||||
|
||||
var (uri, request) = BuildHttpRequest(url, method, content);
|
||||
var response = await client.SendAsync(request);
|
||||
|
|
@ -69,18 +68,12 @@ public class HandleHttpRequestFn : IFunctionCallback
|
|||
return response;
|
||||
}
|
||||
|
||||
private void AddRequestHeaders(HttpClient client)
|
||||
private void PrepareRequestHeaders(HttpClient client)
|
||||
{
|
||||
var auth = $"{_context.HttpContext.Request.Headers["Authorization"]}";
|
||||
var origin = $"{_context.HttpContext.Request.Headers["Origin"]}";
|
||||
|
||||
client.DefaultRequestHeaders.Add("Authorization", auth);
|
||||
|
||||
var settings = _services.GetRequiredService<HttpHandlerSettings>();
|
||||
origin = !string.IsNullOrEmpty(origin) ? origin : settings.Origin;
|
||||
if (!string.IsNullOrEmpty(origin))
|
||||
var hooks = _services.GetServices<IHttpRequestHook>();
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
client.DefaultRequestHeaders.Add("Origin", origin);
|
||||
hook.OnAddHttpHeaders(client.DefaultRequestHeaders);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
using BotSharp.Abstraction.Http;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
namespace BotSharp.Plugin.HttpHandler.Hooks;
|
||||
|
||||
public class BasicHttpRequestHook : IHttpRequestHook
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly IHttpContextAccessor _context;
|
||||
|
||||
private const string AUTHORIZATION = "Authorization";
|
||||
private const string ORIGIN = "Origin";
|
||||
|
||||
public BasicHttpRequestHook(
|
||||
IServiceProvider services,
|
||||
IHttpContextAccessor context)
|
||||
{
|
||||
_services = services;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public void OnAddHttpHeaders(HttpHeaders headers)
|
||||
{
|
||||
var settings = _services.GetRequiredService<HttpHandlerSettings>();
|
||||
|
||||
var auth = $"{_context.HttpContext.Request.Headers[AUTHORIZATION]}";
|
||||
if (!string.IsNullOrEmpty(auth))
|
||||
{
|
||||
headers.Add(AUTHORIZATION, auth);
|
||||
}
|
||||
|
||||
var origin = $"{_context.HttpContext.Request.Headers[ORIGIN]}";
|
||||
origin = !string.IsNullOrEmpty(settings.Origin) ? settings.Origin : origin;
|
||||
if (!string.IsNullOrEmpty(origin))
|
||||
{
|
||||
headers.Add(ORIGIN, origin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
using BotSharp.Abstraction.Agents;
|
||||
using BotSharp.Abstraction.Http;
|
||||
using BotSharp.Abstraction.Settings;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
|
|
@ -21,5 +22,6 @@ public class HttpHandlerPlugin : IBotSharpPlugin
|
|||
});
|
||||
|
||||
services.AddScoped<IAgentUtilityHook, HttpHandlerUtilityHook>();
|
||||
services.AddScoped<IHttpRequestHook, BasicHttpRequestHook>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue