2022-11-23 10:20:00 +00:00
|
|
|
using Elsa.Common.Features;
|
2022-12-31 15:16:43 +00:00
|
|
|
using Elsa.Extensions;
|
2022-05-27 10:36:39 +00:00
|
|
|
using Elsa.Features.Abstractions;
|
2022-11-23 10:20:00 +00:00
|
|
|
using Elsa.Features.Attributes;
|
2022-05-27 10:36:39 +00:00
|
|
|
using Elsa.Features.Services;
|
2022-10-25 13:52:55 +00:00
|
|
|
using Elsa.Http.ContentWriters;
|
2023-03-03 21:47:23 +00:00
|
|
|
using Elsa.Http.Contracts;
|
2023-09-16 09:06:56 +00:00
|
|
|
using Elsa.Http.DownloadableProviders;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Elsa.Http.Handlers;
|
2023-04-17 13:23:05 +00:00
|
|
|
using Elsa.Http.HostedServices;
|
2023-03-07 13:54:21 +00:00
|
|
|
using Elsa.Http.Models;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Elsa.Http.Options;
|
2022-10-25 13:52:55 +00:00
|
|
|
using Elsa.Http.Parsers;
|
2023-03-11 21:14:39 +00:00
|
|
|
using Elsa.Http.PortResolvers;
|
2023-01-24 21:39:31 +00:00
|
|
|
using Elsa.Http.Providers;
|
2023-08-23 08:48:12 +00:00
|
|
|
using Elsa.Http.Selectors;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Elsa.Http.Services;
|
2023-02-15 11:09:45 +00:00
|
|
|
using Elsa.JavaScript.Features;
|
|
|
|
|
using Elsa.Liquid.Features;
|
2023-03-11 21:14:39 +00:00
|
|
|
using Elsa.Workflows.Core.Contracts;
|
2023-07-14 17:19:41 +00:00
|
|
|
using Elsa.Workflows.Management.Requests;
|
|
|
|
|
using Elsa.Workflows.Management.Responses;
|
2023-01-16 18:42:45 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
2023-09-16 09:43:34 +00:00
|
|
|
using Microsoft.AspNetCore.StaticFiles;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
2022-05-27 10:36:39 +00:00
|
|
|
namespace Elsa.Http.Features;
|
2022-05-26 10:47:31 +00:00
|
|
|
|
2023-02-15 11:09:45 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Installs services related to HTTP services and activities.
|
|
|
|
|
/// </summary>
|
2022-11-23 10:20:00 +00:00
|
|
|
[DependsOn(typeof(MemoryCacheFeature))]
|
2023-02-15 11:09:45 +00:00
|
|
|
[DependsOn(typeof(LiquidFeature))]
|
|
|
|
|
[DependsOn(typeof(JavaScriptFeature))]
|
2022-05-27 10:36:39 +00:00
|
|
|
public class HttpFeature : FeatureBase
|
2022-05-26 10:47:31 +00:00
|
|
|
{
|
2023-02-15 11:09:45 +00:00
|
|
|
/// <inheritdoc />
|
2022-05-27 10:36:39 +00:00
|
|
|
public HttpFeature(IModule module) : base(module)
|
2022-05-26 10:47:31 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 18:42:45 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate to configure <see cref="HttpActivityOptions"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<HttpActivityOptions>? ConfigureHttpOptions { get; set; }
|
|
|
|
|
|
2023-02-15 11:09:45 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate that is invoked when authorizing an inbound HTTP request.
|
|
|
|
|
/// </summary>
|
2023-05-01 10:06:51 +00:00
|
|
|
public Func<IServiceProvider, IHttpEndpointAuthorizationHandler> HttpEndpointAuthorizationHandler { get; set; } = sp => sp.GetRequiredService<AllowAnonymousHttpEndpointAuthorizationHandler>();
|
2023-03-08 08:49:43 +00:00
|
|
|
|
2023-02-15 11:09:45 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate that is invoked when an HTTP workflow faults.
|
|
|
|
|
/// </summary>
|
2023-05-01 10:06:51 +00:00
|
|
|
public Func<IServiceProvider, IHttpEndpointWorkflowFaultHandler> HttpEndpointWorkflowFaultHandler { get; set; } = sp => sp.GetRequiredService<DefaultHttpEndpointWorkflowFaultHandler>();
|
2023-09-16 09:43:34 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate to configure the <see cref="IContentTypeProvider"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Func<IServiceProvider, IContentTypeProvider> ContentTypeProvider { get; set; } = _ => new FileExtensionContentTypeProvider();
|
2022-05-26 10:47:31 +00:00
|
|
|
|
2023-01-16 18:42:45 +00:00
|
|
|
/// <summary>
|
2023-03-07 12:45:58 +00:00
|
|
|
/// A delegate to configure the <see cref="HttpClient"/> used when by the <see cref="FlowSendHttpRequest"/> activity.
|
2023-01-16 18:42:45 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public Action<IServiceProvider, HttpClient> HttpClient { get; set; } = (_, _) => { };
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate to configure the <see cref="HttpClientBuilder"/> for <see cref="HttpClient"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<IHttpClientBuilder> HttpClientBuilder { get; set; } = _ => { };
|
|
|
|
|
|
2023-08-23 08:48:12 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A list of <see cref="IHttpCorrelationIdSelector"/> types to register with the service collection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ICollection<Type> HttpCorrelationIdSelectorTypes { get; } = new List<Type>
|
|
|
|
|
{
|
|
|
|
|
typeof(HeaderHttpCorrelationIdSelector),
|
|
|
|
|
typeof(QueryStringHttpCorrelationIdSelector)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A list of <see cref="IHttpWorkflowInstanceIdSelector"/> types to register with the service collection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ICollection<Type> HttpWorkflowInstanceIdSelectorTypes { get; } = new List<Type>
|
|
|
|
|
{
|
|
|
|
|
typeof(HeaderHttpWorkflowInstanceIdSelector),
|
|
|
|
|
typeof(QueryStringHttpWorkflowInstanceIdSelector)
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-27 12:28:43 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Configure()
|
2022-05-26 10:47:31 +00:00
|
|
|
{
|
2023-01-16 18:42:45 +00:00
|
|
|
Module.UseWorkflowManagement(management =>
|
2022-11-27 12:28:43 +00:00
|
|
|
{
|
2023-01-16 18:42:45 +00:00
|
|
|
management.AddVariableTypes(new[]
|
|
|
|
|
{
|
|
|
|
|
typeof(RouteData),
|
|
|
|
|
typeof(HttpRequest),
|
2023-03-07 13:54:21 +00:00
|
|
|
typeof(HttpResponse),
|
2023-04-10 13:32:40 +00:00
|
|
|
typeof(HttpResponseMessage),
|
2023-03-07 13:54:21 +00:00
|
|
|
typeof(HttpRequestHeaders)
|
2023-01-16 18:42:45 +00:00
|
|
|
}, "HTTP");
|
2023-03-08 08:49:43 +00:00
|
|
|
|
2023-01-16 18:42:45 +00:00
|
|
|
management.AddActivitiesFrom<HttpFeature>();
|
|
|
|
|
});
|
2023-08-23 08:48:12 +00:00
|
|
|
|
2023-07-14 17:19:41 +00:00
|
|
|
Services.AddRequestHandler<ValidateWorkflowRequestHandler, ValidateWorkflowRequest, ValidateWorkflowResponse>();
|
2022-05-26 10:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
2023-04-17 13:23:05 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void ConfigureHostedServices()
|
|
|
|
|
{
|
|
|
|
|
ConfigureHostedService<UpdateRouteTableHostedService>();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-27 12:28:43 +00:00
|
|
|
/// <inheritdoc />
|
2022-05-26 10:47:31 +00:00
|
|
|
public override void Apply()
|
|
|
|
|
{
|
2022-11-19 21:30:43 +00:00
|
|
|
var configureOptions = ConfigureHttpOptions ?? (options =>
|
|
|
|
|
{
|
|
|
|
|
options.BasePath = "/workflows";
|
|
|
|
|
options.BaseUrl = new Uri("http://localhost");
|
|
|
|
|
});
|
2023-01-16 18:42:45 +00:00
|
|
|
|
2022-11-19 21:30:43 +00:00
|
|
|
Services.Configure(configureOptions);
|
2022-05-26 10:47:31 +00:00
|
|
|
|
2023-03-07 12:45:58 +00:00
|
|
|
var httpClientBuilder = Services.AddHttpClient<SendHttpRequestBase>(HttpClient);
|
2023-01-16 18:42:45 +00:00
|
|
|
HttpClientBuilder(httpClientBuilder);
|
|
|
|
|
|
2022-05-26 10:47:31 +00:00
|
|
|
Services
|
|
|
|
|
.AddSingleton<IRouteMatcher, RouteMatcher>()
|
|
|
|
|
.AddSingleton<IRouteTable, RouteTable>()
|
2022-11-19 21:30:43 +00:00
|
|
|
.AddSingleton<IAbsoluteUrlProvider, DefaultAbsoluteUrlProvider>()
|
2023-01-24 20:03:50 +00:00
|
|
|
.AddSingleton<IHttpBookmarkProcessor, HttpBookmarkProcessor>()
|
2023-05-24 12:21:19 +00:00
|
|
|
.AddSingleton<IRouteTableUpdater, DefaultRouteTableUpdater>()
|
2023-09-16 09:43:34 +00:00
|
|
|
.AddSingleton(ContentTypeProvider)
|
2022-05-26 10:47:31 +00:00
|
|
|
.AddNotificationHandlersFrom<UpdateRouteTable>()
|
2022-10-25 13:52:55 +00:00
|
|
|
.AddHttpContextAccessor()
|
|
|
|
|
|
2023-01-24 21:39:31 +00:00
|
|
|
// Content parsers.
|
2023-01-16 18:42:45 +00:00
|
|
|
.AddSingleton<IHttpContentParser, StringHttpContentParser>()
|
|
|
|
|
.AddSingleton<IHttpContentParser, JsonHttpContentParser>()
|
|
|
|
|
.AddSingleton<IHttpContentParser, XmlHttpContentParser>()
|
|
|
|
|
|
2023-01-24 21:39:31 +00:00
|
|
|
// HTTP content factories.
|
2023-01-24 20:48:05 +00:00
|
|
|
.AddSingleton<IHttpContentFactory, TextContentFactory>()
|
|
|
|
|
.AddSingleton<IHttpContentFactory, JsonContentFactory>()
|
2023-01-24 21:39:31 +00:00
|
|
|
.AddSingleton<IHttpContentFactory, XmlContentFactory>()
|
2023-01-24 20:48:05 +00:00
|
|
|
.AddSingleton<IHttpContentFactory, FormUrlEncodedHttpContentFactory>()
|
2023-03-02 09:27:11 +00:00
|
|
|
|
2023-02-15 11:09:45 +00:00
|
|
|
// Activity property options providers.
|
2023-01-24 21:39:31 +00:00
|
|
|
.AddSingleton<IActivityPropertyOptionsProvider, WriteHttpResponseContentTypeOptionsProvider>()
|
2023-05-11 20:58:59 +00:00
|
|
|
|
2023-03-11 21:14:39 +00:00
|
|
|
// Port resolvers.
|
|
|
|
|
.AddSingleton<IActivityPortResolver, SendHttpRequestActivityPortResolver>()
|
2023-03-02 09:27:11 +00:00
|
|
|
|
2023-09-16 09:06:56 +00:00
|
|
|
// HTTP endpoint handlers.
|
2023-05-01 10:06:51 +00:00
|
|
|
.AddSingleton<AuthenticationBasedHttpEndpointAuthorizationHandler>()
|
|
|
|
|
.AddSingleton<AllowAnonymousHttpEndpointAuthorizationHandler>()
|
|
|
|
|
.AddSingleton<DefaultHttpEndpointWorkflowFaultHandler>()
|
2023-03-02 09:27:11 +00:00
|
|
|
.AddSingleton(HttpEndpointWorkflowFaultHandler)
|
2023-03-08 08:49:43 +00:00
|
|
|
.AddSingleton(HttpEndpointAuthorizationHandler)
|
2023-09-16 09:06:56 +00:00
|
|
|
|
|
|
|
|
// File related services.
|
|
|
|
|
.AddSingleton<IDownloadableManager, DefaultDownloadableManager>()
|
|
|
|
|
.AddSingleton<IDownloadableProvider, BinaryDownloadableProvider>()
|
|
|
|
|
.AddSingleton<IDownloadableProvider, DownloadableDownloadableProvider>()
|
|
|
|
|
.AddSingleton<IDownloadableProvider, MultiDownloadableProvider>()
|
|
|
|
|
.AddSingleton<IDownloadableProvider, StreamDownloadableProvider>()
|
|
|
|
|
.AddSingleton<IDownloadableProvider, UrlDownloadableProvider>()
|
2023-05-11 20:58:59 +00:00
|
|
|
|
2023-03-07 13:54:21 +00:00
|
|
|
// Add mediator handlers.
|
2023-05-11 20:58:59 +00:00
|
|
|
.AddNotificationHandlersFrom<HttpFeature>()
|
|
|
|
|
|
|
|
|
|
// AuthenticationBasedHttpEndpointAuthorizationHandler requires Authorization services.
|
|
|
|
|
// We could consider creating a separate module for installing authorization services.
|
|
|
|
|
.AddAuthorization();
|
2023-09-16 09:06:56 +00:00
|
|
|
|
|
|
|
|
// HTTP clients.
|
|
|
|
|
Services.AddHttpClient<IFileDownloader, HttpClientFileDownloader>();
|
2023-08-23 08:48:12 +00:00
|
|
|
|
|
|
|
|
// Add selectors.
|
|
|
|
|
foreach (var httpCorrelationIdSelectorType in HttpCorrelationIdSelectorTypes)
|
|
|
|
|
Services.AddSingleton(typeof(IHttpCorrelationIdSelector), httpCorrelationIdSelectorType);
|
|
|
|
|
|
|
|
|
|
foreach (var httpWorkflowInstanceIdSelectorType in HttpWorkflowInstanceIdSelectorTypes)
|
|
|
|
|
Services.AddSingleton(typeof(IHttpWorkflowInstanceIdSelector), httpWorkflowInstanceIdSelectorType);
|
2022-05-26 10:47:31 +00:00
|
|
|
}
|
|
|
|
|
}
|