| | | 1 | | using Elsa.Expressions.Options; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Features.Abstractions; |
| | | 4 | | using Elsa.Features.Attributes; |
| | | 5 | | using Elsa.Features.Services; |
| | | 6 | | using Elsa.Http.Bookmarks; |
| | | 7 | | using Elsa.Http.ContentWriters; |
| | | 8 | | using Elsa.Http.DownloadableContentHandlers; |
| | | 9 | | using Elsa.Http.FileCaches; |
| | | 10 | | using Elsa.Http.Handlers; |
| | | 11 | | using Elsa.Http.Options; |
| | | 12 | | using Elsa.Http.Parsers; |
| | | 13 | | using Elsa.Http.PortResolvers; |
| | | 14 | | using Elsa.Http.Resilience; |
| | | 15 | | using Elsa.Http.Selectors; |
| | | 16 | | using Elsa.Http.Services; |
| | | 17 | | using Elsa.Http.Tasks; |
| | | 18 | | using Elsa.Http.TriggerPayloadValidators; |
| | | 19 | | using Elsa.Http.UIHints; |
| | | 20 | | using Elsa.Resilience.Extensions; |
| | | 21 | | using Elsa.Resilience.Features; |
| | | 22 | | using Elsa.Workflows; |
| | | 23 | | using FluentStorage; |
| | | 24 | | using Microsoft.AspNetCore.Http; |
| | | 25 | | using Microsoft.AspNetCore.StaticFiles; |
| | | 26 | | using Microsoft.Extensions.DependencyInjection; |
| | | 27 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 28 | | using Microsoft.Extensions.Options; |
| | | 29 | | |
| | | 30 | | namespace Elsa.Http.Features; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Installs services related to HTTP services and activities. |
| | | 34 | | /// </summary> |
| | | 35 | | [DependsOn(typeof(HttpJavaScriptFeature))] |
| | | 36 | | [DependsOn(typeof(ResilienceFeature))] |
| | 5 | 37 | | public class HttpFeature(IModule module) : FeatureBase(module) |
| | | 38 | | { |
| | 328 | 39 | | private Func<IServiceProvider, IHttpEndpointRoutesProvider> _httpEndpointRouteProvider = sp => sp.GetRequiredService |
| | 1127 | 40 | | private Func<IServiceProvider, IHttpEndpointBasePathProvider> _httpEndpointBasePathProvider = sp => sp.GetRequiredSe |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// A delegate to configure <see cref="HttpActivityOptions"/>. |
| | | 44 | | /// </summary> |
| | 6 | 45 | | public Action<HttpActivityOptions>? ConfigureHttpOptions { get; set; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// A delegate to configure <see cref="HttpFileCacheOptions"/>. |
| | | 49 | | /// </summary> |
| | 5 | 50 | | public Action<HttpFileCacheOptions>? ConfigureHttpFileCacheOptions { get; set; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// A delegate that is invoked when authorizing an inbound HTTP request. |
| | | 54 | | /// </summary> |
| | 224 | 55 | | public Func<IServiceProvider, IHttpEndpointAuthorizationHandler> HttpEndpointAuthorizationHandler { get; set; } = sp |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// A delegate that is invoked when an HTTP workflow faults. |
| | | 59 | | /// </summary> |
| | 10 | 60 | | public Func<IServiceProvider, IHttpEndpointFaultHandler> HttpEndpointWorkflowFaultHandler { get; set; } = sp => sp.G |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// A delegate to configure the <see cref="IContentTypeProvider"/>. |
| | | 64 | | /// </summary> |
| | 10 | 65 | | public Func<IServiceProvider, IContentTypeProvider> ContentTypeProvider { get; set; } = _ => new FileExtensionConten |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// A delegate to configure the <see cref="IFileCacheStorageProvider"/>. |
| | | 69 | | /// </summary> |
| | 10 | 70 | | public Func<IServiceProvider, IFileCacheStorageProvider> FileCache { get; set; } = sp => |
| | 5 | 71 | | { |
| | 0 | 72 | | var options = sp.GetRequiredService<IOptions<HttpFileCacheOptions>>().Value; |
| | 0 | 73 | | var blobStorage = StorageFactory.Blobs.DirectoryFiles(options.LocalCacheDirectory); |
| | 0 | 74 | | return new BlobFileCacheStorageProvider(blobStorage); |
| | 5 | 75 | | }; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// A delegate to configure the <see cref="HttpClient"/> used when by the <see cref="FlowSendHttpRequest"/> and <see |
| | | 79 | | /// </summary> |
| | 11 | 80 | | public Action<IServiceProvider, HttpClient> HttpClient { get; set; } = (_, _) => { }; |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// A delegate to configure the <see cref="HttpClientBuilder"/> for <see cref="HttpClient"/>. |
| | | 84 | | /// </summary> |
| | 15 | 85 | | public Action<IHttpClientBuilder> HttpClientBuilder { get; set; } = _ => { }; |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// A list of <see cref="IHttpCorrelationIdSelector"/> types to register with the service collection. |
| | | 89 | | /// </summary> |
| | 10 | 90 | | public ICollection<Type> HttpCorrelationIdSelectorTypes { get; } = new List<Type> |
| | 5 | 91 | | { |
| | 5 | 92 | | typeof(HeaderHttpCorrelationIdSelector), |
| | 5 | 93 | | typeof(QueryStringHttpCorrelationIdSelector) |
| | 5 | 94 | | }; |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// A list of <see cref="IHttpWorkflowInstanceIdSelector"/> types to register with the service collection. |
| | | 98 | | /// </summary> |
| | 10 | 99 | | public ICollection<Type> HttpWorkflowInstanceIdSelectorTypes { get; } = new List<Type> |
| | 5 | 100 | | { |
| | 5 | 101 | | typeof(HeaderHttpWorkflowInstanceIdSelector), |
| | 5 | 102 | | typeof(QueryStringHttpWorkflowInstanceIdSelector) |
| | 5 | 103 | | }; |
| | | 104 | | |
| | | 105 | | public HttpFeature WithHttpEndpointRoutesProvider<T>() where T : IHttpEndpointRoutesProvider |
| | | 106 | | { |
| | 0 | 107 | | return WithHttpEndpointRoutesProvider(sp => sp.GetRequiredService<T>()); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public HttpFeature WithHttpEndpointRoutesProvider(Func<IServiceProvider, IHttpEndpointRoutesProvider> httpEndpointRo |
| | | 111 | | { |
| | 0 | 112 | | _httpEndpointRouteProvider = httpEndpointRouteProvider; |
| | 0 | 113 | | return this; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | public HttpFeature WithHttpEndpointBasePathProvider<T>() where T : class, IHttpEndpointBasePathProvider |
| | | 117 | | { |
| | 0 | 118 | | Services.TryAddScoped<T>(); |
| | 0 | 119 | | return WithHttpEndpointBasePathProvider(sp => sp.GetRequiredService<T>()); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | public HttpFeature WithHttpEndpointBasePathProvider(Func<IServiceProvider, IHttpEndpointBasePathProvider> httpEndpoi |
| | | 123 | | { |
| | 0 | 124 | | _httpEndpointBasePathProvider = httpEndpointBasePathProvider; |
| | 0 | 125 | | return this; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <inheritdoc /> |
| | | 129 | | public override void Configure() |
| | | 130 | | { |
| | 5 | 131 | | Module.UseWorkflowManagement(management => |
| | 5 | 132 | | { |
| | 5 | 133 | | management.AddVariableTypes([ |
| | 5 | 134 | | typeof(HttpRouteData), |
| | 5 | 135 | | typeof(HttpRequest), |
| | 5 | 136 | | typeof(HttpResponse), |
| | 5 | 137 | | typeof(HttpResponseMessage), |
| | 5 | 138 | | typeof(HttpHeaders), |
| | 5 | 139 | | typeof(IFormFile), |
| | 5 | 140 | | typeof(HttpFile), |
| | 5 | 141 | | typeof(Downloadable) |
| | 5 | 142 | | ], "HTTP"); |
| | 5 | 143 | | |
| | 5 | 144 | | management.AddActivitiesFrom<HttpFeature>(); |
| | 10 | 145 | | }); |
| | | 146 | | |
| | 10 | 147 | | Module.UseResilience(resilience => resilience.AddResilienceStrategyType<HttpResilienceStrategy>()); |
| | 5 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <inheritdoc /> |
| | | 151 | | public override void Apply() |
| | | 152 | | { |
| | 5 | 153 | | var configureOptions = ConfigureHttpOptions ?? (options => |
| | 5 | 154 | | { |
| | 4 | 155 | | options.BasePath = "/workflows"; |
| | 4 | 156 | | options.BaseUrl = new Uri("http://localhost"); |
| | 9 | 157 | | }); |
| | | 158 | | |
| | 5 | 159 | | var configureFileCacheOptions = ConfigureHttpFileCacheOptions ?? (options => { options.TimeToLive = TimeSpan.Fro |
| | | 160 | | |
| | 5 | 161 | | Services.Configure(configureOptions); |
| | 5 | 162 | | Services.Configure(configureFileCacheOptions); |
| | | 163 | | |
| | 5 | 164 | | var httpClientBuilder = Services.AddHttpClient<SendHttpRequestBase>(HttpClient); |
| | 5 | 165 | | HttpClientBuilder(httpClientBuilder); |
| | | 166 | | |
| | 5 | 167 | | Services |
| | 5 | 168 | | .AddScoped<IRouteMatcher, RouteMatcher>() |
| | 5 | 169 | | .AddScoped<IRouteTable, RouteTable>() |
| | 5 | 170 | | .AddScoped<IAbsoluteUrlProvider, DefaultAbsoluteUrlProvider>() |
| | 5 | 171 | | .AddScoped<IRouteTableUpdater, DefaultRouteTableUpdater>() |
| | 5 | 172 | | .AddScoped<IHttpWorkflowLookupService, HttpWorkflowLookupService>() |
| | 5 | 173 | | .AddScoped(ContentTypeProvider) |
| | 5 | 174 | | .AddHttpContextAccessor() |
| | 5 | 175 | | |
| | 5 | 176 | | // Handlers. |
| | 5 | 177 | | .AddNotificationHandler<UpdateRouteTable>() |
| | 5 | 178 | | |
| | 5 | 179 | | // Content parsers. |
| | 5 | 180 | | .AddSingleton<IHttpContentParser, JsonHttpContentParser>() |
| | 5 | 181 | | .AddSingleton<IHttpContentParser, XmlHttpContentParser>() |
| | 5 | 182 | | .AddSingleton<IHttpContentParser, PlainTextHttpContentParser>() |
| | 5 | 183 | | .AddSingleton<IHttpContentParser, TextHtmlHttpContentParser>() |
| | 5 | 184 | | .AddSingleton<IHttpContentParser, FileHttpContentParser>() |
| | 5 | 185 | | |
| | 5 | 186 | | // HTTP content factories. |
| | 5 | 187 | | .AddScoped<IHttpContentFactory, TextContentFactory>() |
| | 5 | 188 | | .AddScoped<IHttpContentFactory, JsonContentFactory>() |
| | 5 | 189 | | .AddScoped<IHttpContentFactory, XmlContentFactory>() |
| | 5 | 190 | | .AddScoped<IHttpContentFactory, FormUrlEncodedHttpContentFactory>() |
| | 5 | 191 | | |
| | 5 | 192 | | // Activity property options providers. |
| | 5 | 193 | | .AddScoped<IPropertyUIHandler, HttpContentTypeOptionsProvider>() |
| | 5 | 194 | | .AddScoped<IPropertyUIHandler, HttpEndpointPathUIHandler>() |
| | 5 | 195 | | .AddScoped(_httpEndpointBasePathProvider) |
| | 5 | 196 | | |
| | 5 | 197 | | // Port resolvers. |
| | 5 | 198 | | .AddScoped<IActivityResolver, SendHttpRequestActivityResolver>() |
| | 5 | 199 | | |
| | 5 | 200 | | // HTTP endpoint handlers. |
| | 5 | 201 | | .AddScoped<AuthenticationBasedHttpEndpointAuthorizationHandler>() |
| | 5 | 202 | | .AddScoped<AllowAnonymousHttpEndpointAuthorizationHandler>() |
| | 5 | 203 | | .AddScoped<DefaultHttpEndpointFaultHandler>() |
| | 5 | 204 | | .AddScoped<DefaultHttpEndpointRoutesProvider>() |
| | 5 | 205 | | .AddScoped<DefaultHttpEndpointBasePathProvider>() |
| | 5 | 206 | | .AddScoped(HttpEndpointWorkflowFaultHandler) |
| | 5 | 207 | | .AddScoped(HttpEndpointAuthorizationHandler) |
| | 5 | 208 | | .AddScoped(_httpEndpointRouteProvider) |
| | 5 | 209 | | |
| | 5 | 210 | | // Startup tasks. |
| | 5 | 211 | | .AddStartupTask<UpdateRouteTableStartupTask>() |
| | 5 | 212 | | |
| | 5 | 213 | | // Downloadable content handlers. |
| | 5 | 214 | | .AddScoped<IDownloadableManager, DefaultDownloadableManager>() |
| | 5 | 215 | | .AddScoped<IDownloadableContentHandler, MultiDownloadableContentHandler>() |
| | 5 | 216 | | .AddScoped<IDownloadableContentHandler, BinaryDownloadableContentHandler>() |
| | 5 | 217 | | .AddScoped<IDownloadableContentHandler, StreamDownloadableContentHandler>() |
| | 5 | 218 | | .AddScoped<IDownloadableContentHandler, FormFileDownloadableContentHandler>() |
| | 5 | 219 | | .AddScoped<IDownloadableContentHandler, DownloadableDownloadableContentHandler>() |
| | 5 | 220 | | .AddScoped<IDownloadableContentHandler, UrlDownloadableContentHandler>() |
| | 5 | 221 | | .AddScoped<IDownloadableContentHandler, StringDownloadableContentHandler>() |
| | 5 | 222 | | .AddScoped<IDownloadableContentHandler, HttpFileDownloadableContentHandler>() |
| | 5 | 223 | | |
| | 5 | 224 | | //Trigger payload validators. |
| | 5 | 225 | | .AddTriggerPayloadValidator<HttpEndpointTriggerPayloadValidator, HttpEndpointBookmarkPayload>() |
| | 5 | 226 | | |
| | 5 | 227 | | // File caches. |
| | 5 | 228 | | .AddScoped(FileCache) |
| | 5 | 229 | | .AddScoped<ZipManager>() |
| | 5 | 230 | | |
| | 5 | 231 | | // AuthenticationBasedHttpEndpointAuthorizationHandler requires Authorization services. |
| | 5 | 232 | | // We could consider creating a separate module for installing authorization services. |
| | 5 | 233 | | .AddAuthorization(); |
| | | 234 | | |
| | | 235 | | // HTTP clients. |
| | 5 | 236 | | Services.AddHttpClient<IFileDownloader, HttpClientFileDownloader>(); |
| | | 237 | | |
| | | 238 | | // Add selectors. |
| | 30 | 239 | | foreach (var httpCorrelationIdSelectorType in HttpCorrelationIdSelectorTypes) |
| | 10 | 240 | | Services.AddScoped(typeof(IHttpCorrelationIdSelector), httpCorrelationIdSelectorType); |
| | | 241 | | |
| | 30 | 242 | | foreach (var httpWorkflowInstanceIdSelectorType in HttpWorkflowInstanceIdSelectorTypes) |
| | 10 | 243 | | Services.AddScoped(typeof(IHttpWorkflowInstanceIdSelector), httpWorkflowInstanceIdSelectorType); |
| | | 244 | | |
| | 5 | 245 | | Services.Configure<ExpressionOptions>(options => |
| | 5 | 246 | | { |
| | 5 | 247 | | options.AddTypeAlias<HttpRequest>("HttpRequest"); |
| | 5 | 248 | | options.AddTypeAlias<HttpResponse>("HttpResponse"); |
| | 5 | 249 | | options.AddTypeAlias<HttpResponseMessage>("HttpResponseMessage"); |
| | 5 | 250 | | options.AddTypeAlias<HttpHeaders>("HttpHeaders"); |
| | 5 | 251 | | options.AddTypeAlias<HttpRouteData>("RouteData"); |
| | 5 | 252 | | options.AddTypeAlias<IFormFile>("FormFile"); |
| | 5 | 253 | | options.AddTypeAlias<IFormFile[]>("FormFile[]"); |
| | 5 | 254 | | options.AddTypeAlias<HttpFile>("HttpFile"); |
| | 5 | 255 | | options.AddTypeAlias<HttpFile[]>("HttpFile[]"); |
| | 5 | 256 | | options.AddTypeAlias<Downloadable>("Downloadable"); |
| | 5 | 257 | | options.AddTypeAlias<Downloadable[]>("Downloadable[]"); |
| | 10 | 258 | | }); |
| | 5 | 259 | | } |
| | | 260 | | } |