| | | 1 | | using CShells.AspNetCore.Features; |
| | | 2 | | using CShells.Features; |
| | | 3 | | using Elsa.Expressions.Options; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Http.Bookmarks; |
| | | 6 | | using Elsa.Http.ContentWriters; |
| | | 7 | | using Elsa.Http.DownloadableContentHandlers; |
| | | 8 | | using Elsa.Http.FileCaches; |
| | | 9 | | using Elsa.Http.Handlers; |
| | | 10 | | using Elsa.Http.Middleware; |
| | | 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.Workflows; |
| | | 22 | | using Elsa.Workflows.Management.Extensions; |
| | | 23 | | using FluentStorage; |
| | | 24 | | using JetBrains.Annotations; |
| | | 25 | | using Microsoft.AspNetCore.Builder; |
| | | 26 | | using Microsoft.AspNetCore.Http; |
| | | 27 | | using Microsoft.AspNetCore.Routing; |
| | | 28 | | using Microsoft.AspNetCore.StaticFiles; |
| | | 29 | | using Microsoft.Extensions.DependencyInjection; |
| | | 30 | | using Microsoft.Extensions.Hosting; |
| | | 31 | | using Microsoft.Extensions.Options; |
| | | 32 | | |
| | | 33 | | namespace Elsa.Http.ShellFeatures; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Installs services related to HTTP services and activities. |
| | | 37 | | /// </summary> |
| | | 38 | | [ShellFeature( |
| | | 39 | | DisplayName = "HTTP", |
| | | 40 | | Description = "Provides HTTP-related activities and services for workflow execution", |
| | | 41 | | DependsOn = ["HttpJavaScript", "Resilience"])] |
| | | 42 | | [UsedImplicitly] |
| | | 43 | | public class HttpFeature : IMiddlewareShellFeature |
| | | 44 | | { |
| | | 45 | | /// <summary> |
| | | 46 | | /// The <see cref="HttpActivityOptions"/> to configure. |
| | | 47 | | /// </summary> |
| | 0 | 48 | | public HttpActivityOptions HttpActivityOptions { get; set; } = new(); |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// A delegate to configure <see cref="HttpFileCacheOptions"/>. |
| | | 52 | | /// </summary> |
| | 0 | 53 | | public Action<HttpFileCacheOptions>? ConfigureHttpFileCacheOptions { get; set; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// A delegate that is invoked when authorizing an inbound HTTP request. |
| | | 57 | | /// </summary> |
| | 0 | 58 | | public Func<IServiceProvider, IHttpEndpointAuthorizationHandler> HttpEndpointAuthorizationHandler { get; set; } = sp |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// A delegate that is invoked when an HTTP workflow faults. |
| | | 62 | | /// </summary> |
| | 0 | 63 | | public Func<IServiceProvider, IHttpEndpointFaultHandler> HttpEndpointWorkflowFaultHandler { get; set; } = sp => sp.G |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// A delegate to configure the <see cref="IContentTypeProvider"/>. |
| | | 67 | | /// </summary> |
| | 0 | 68 | | public Func<IServiceProvider, IContentTypeProvider> ContentTypeProvider { get; set; } = _ => new FileExtensionConten |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// A delegate to configure the <see cref="IFileCacheStorageProvider"/>. |
| | | 72 | | /// </summary> |
| | 0 | 73 | | public Func<IServiceProvider, IFileCacheStorageProvider> FileCache { get; set; } = sp => |
| | 0 | 74 | | { |
| | 0 | 75 | | var options = sp.GetRequiredService<IOptions<HttpFileCacheOptions>>().Value; |
| | 0 | 76 | | var blobStorage = StorageFactory.Blobs.DirectoryFiles(options.LocalCacheDirectory); |
| | 0 | 77 | | return new BlobFileCacheStorageProvider(blobStorage); |
| | 0 | 78 | | }; |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// A delegate to configure the <see cref="HttpClient"/> used when by the <see cref="FlowSendHttpRequest"/> and <see |
| | | 82 | | /// </summary> |
| | 0 | 83 | | public Action<IServiceProvider, HttpClient> HttpClient { get; set; } = (_, _) => { }; |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// A delegate to configure the <see cref="HttpClientBuilder"/> for <see cref="HttpClient"/>. |
| | | 87 | | /// </summary> |
| | 0 | 88 | | public Action<IHttpClientBuilder> HttpClientBuilder { get; set; } = _ => { }; |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// A list of <see cref="IHttpCorrelationIdSelector"/> types to register with the service collection. |
| | | 92 | | /// </summary> |
| | 0 | 93 | | public ICollection<Type> HttpCorrelationIdSelectorTypes { get; } = new List<Type> |
| | 0 | 94 | | { |
| | 0 | 95 | | typeof(HeaderHttpCorrelationIdSelector), |
| | 0 | 96 | | typeof(QueryStringHttpCorrelationIdSelector) |
| | 0 | 97 | | }; |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// A list of <see cref="IHttpWorkflowInstanceIdSelector"/> types to register with the service collection. |
| | | 101 | | /// </summary> |
| | 0 | 102 | | public ICollection<Type> HttpWorkflowInstanceIdSelectorTypes { get; } = new List<Type> |
| | 0 | 103 | | { |
| | 0 | 104 | | typeof(HeaderHttpWorkflowInstanceIdSelector), |
| | 0 | 105 | | typeof(QueryStringHttpWorkflowInstanceIdSelector) |
| | 0 | 106 | | }; |
| | | 107 | | |
| | | 108 | | public void ConfigureServices(IServiceCollection services) |
| | | 109 | | { |
| | | 110 | | // Register HTTP activities. |
| | 0 | 111 | | services.AddActivitiesFrom<HttpFeature>(); |
| | | 112 | | |
| | | 113 | | // Register HTTP variable types. |
| | 0 | 114 | | services.AddVariableDescriptors([ |
| | 0 | 115 | | new(typeof(HttpRouteData), "HTTP", null), |
| | 0 | 116 | | new(typeof(HttpRequest), "HTTP", null), |
| | 0 | 117 | | new(typeof(HttpResponse), "HTTP", null), |
| | 0 | 118 | | new(typeof(HttpResponseMessage), "HTTP", null), |
| | 0 | 119 | | new(typeof(HttpHeaders), "HTTP", null), |
| | 0 | 120 | | new(typeof(IFormFile), "HTTP", null), |
| | 0 | 121 | | new(typeof(HttpFile), "HTTP", null), |
| | 0 | 122 | | new(typeof(Downloadable), "HTTP", null), |
| | 0 | 123 | | ]); |
| | | 124 | | |
| | | 125 | | // Register the HTTP resilience strategy. |
| | 0 | 126 | | services.AddResilienceStrategy<HttpResilienceStrategy>(); |
| | | 127 | | |
| | 0 | 128 | | var configureFileCacheOptions = ConfigureHttpFileCacheOptions ?? (options => { options.TimeToLive = TimeSpan.Fro |
| | | 129 | | |
| | 0 | 130 | | services.Configure<HttpActivityOptions>(options => |
| | 0 | 131 | | { |
| | 0 | 132 | | options.BasePath = HttpActivityOptions.BasePath; |
| | 0 | 133 | | options.BaseUrl = HttpActivityOptions.BaseUrl; |
| | 0 | 134 | | options.AvailableContentTypes = HttpActivityOptions.AvailableContentTypes; |
| | 0 | 135 | | options.WriteHttpResponseSynchronously = HttpActivityOptions.WriteHttpResponseSynchronously; |
| | 0 | 136 | | }); |
| | | 137 | | |
| | 0 | 138 | | services.Configure(configureFileCacheOptions); |
| | | 139 | | |
| | 0 | 140 | | var httpClientBuilder = services.AddHttpClient<SendHttpRequestBase>(HttpClient); |
| | 0 | 141 | | HttpClientBuilder(httpClientBuilder); |
| | | 142 | | |
| | 0 | 143 | | services |
| | 0 | 144 | | .AddScoped<IRouteMatcher, RouteMatcher>() |
| | 0 | 145 | | .AddScoped<IRouteTable, RouteTable>() |
| | 0 | 146 | | .AddScoped<IAbsoluteUrlProvider, DefaultAbsoluteUrlProvider>() |
| | 0 | 147 | | .AddScoped<IRouteTableUpdater, DefaultRouteTableUpdater>() |
| | 0 | 148 | | .AddScoped<IHttpWorkflowLookupService, HttpWorkflowLookupService>() |
| | 0 | 149 | | .AddScoped(ContentTypeProvider) |
| | 0 | 150 | | .AddHttpContextAccessor() |
| | 0 | 151 | | |
| | 0 | 152 | | // Handlers. |
| | 0 | 153 | | .AddNotificationHandler<UpdateRouteTable>() |
| | 0 | 154 | | |
| | 0 | 155 | | // Content parsers. |
| | 0 | 156 | | .AddSingleton<IHttpContentParser, JsonHttpContentParser>() |
| | 0 | 157 | | .AddSingleton<IHttpContentParser, XmlHttpContentParser>() |
| | 0 | 158 | | .AddSingleton<IHttpContentParser, PlainTextHttpContentParser>() |
| | 0 | 159 | | .AddSingleton<IHttpContentParser, TextHtmlHttpContentParser>() |
| | 0 | 160 | | .AddSingleton<IHttpContentParser, FileHttpContentParser>() |
| | 0 | 161 | | |
| | 0 | 162 | | // HTTP content factories. |
| | 0 | 163 | | .AddScoped<IHttpContentFactory, TextContentFactory>() |
| | 0 | 164 | | .AddScoped<IHttpContentFactory, JsonContentFactory>() |
| | 0 | 165 | | .AddScoped<IHttpContentFactory, XmlContentFactory>() |
| | 0 | 166 | | .AddScoped<IHttpContentFactory, FormUrlEncodedHttpContentFactory>() |
| | 0 | 167 | | |
| | 0 | 168 | | // Activity property options providers. |
| | 0 | 169 | | .AddScoped<IPropertyUIHandler, HttpContentTypeOptionsProvider>() |
| | 0 | 170 | | .AddScoped<IPropertyUIHandler, HttpEndpointPathUIHandler>() |
| | 0 | 171 | | |
| | 0 | 172 | | // Default providers. |
| | 0 | 173 | | .AddScoped<DefaultHttpEndpointBasePathProvider>() |
| | 0 | 174 | | .AddScoped<IHttpEndpointBasePathProvider>(sp => sp.GetRequiredService<DefaultHttpEndpointBasePathProvider>() |
| | 0 | 175 | | |
| | 0 | 176 | | // Port resolvers. |
| | 0 | 177 | | .AddScoped<IActivityResolver, SendHttpRequestActivityResolver>() |
| | 0 | 178 | | |
| | 0 | 179 | | // HTTP endpoint handlers. |
| | 0 | 180 | | .AddScoped<AuthenticationBasedHttpEndpointAuthorizationHandler>() |
| | 0 | 181 | | .AddScoped<AllowAnonymousHttpEndpointAuthorizationHandler>() |
| | 0 | 182 | | .AddScoped<DefaultHttpEndpointFaultHandler>() |
| | 0 | 183 | | .AddScoped<DefaultHttpEndpointRoutesProvider>() |
| | 0 | 184 | | .AddScoped(HttpEndpointWorkflowFaultHandler) |
| | 0 | 185 | | .AddScoped(HttpEndpointAuthorizationHandler) |
| | 0 | 186 | | .AddScoped<IHttpEndpointRoutesProvider>(sp => sp.GetRequiredService<DefaultHttpEndpointRoutesProvider>()) |
| | 0 | 187 | | |
| | 0 | 188 | | // Startup tasks. |
| | 0 | 189 | | .AddStartupTask<UpdateRouteTableStartupTask>() |
| | 0 | 190 | | |
| | 0 | 191 | | // Downloadable content handlers. |
| | 0 | 192 | | .AddScoped<IDownloadableManager, DefaultDownloadableManager>() |
| | 0 | 193 | | .AddScoped<IDownloadableContentHandler, MultiDownloadableContentHandler>() |
| | 0 | 194 | | .AddScoped<IDownloadableContentHandler, BinaryDownloadableContentHandler>() |
| | 0 | 195 | | .AddScoped<IDownloadableContentHandler, StreamDownloadableContentHandler>() |
| | 0 | 196 | | .AddScoped<IDownloadableContentHandler, FormFileDownloadableContentHandler>() |
| | 0 | 197 | | .AddScoped<IDownloadableContentHandler, DownloadableDownloadableContentHandler>() |
| | 0 | 198 | | .AddScoped<IDownloadableContentHandler, UrlDownloadableContentHandler>() |
| | 0 | 199 | | .AddScoped<IDownloadableContentHandler, StringDownloadableContentHandler>() |
| | 0 | 200 | | .AddScoped<IDownloadableContentHandler, HttpFileDownloadableContentHandler>() |
| | 0 | 201 | | |
| | 0 | 202 | | //Trigger payload validators. |
| | 0 | 203 | | .AddTriggerPayloadValidator<HttpEndpointTriggerPayloadValidator, HttpEndpointBookmarkPayload>() |
| | 0 | 204 | | |
| | 0 | 205 | | // File caches. |
| | 0 | 206 | | .AddScoped(FileCache) |
| | 0 | 207 | | .AddScoped<ZipManager>() |
| | 0 | 208 | | |
| | 0 | 209 | | // AuthenticationBasedHttpEndpointAuthorizationHandler requires Authorization services. |
| | 0 | 210 | | .AddAuthorization(); |
| | | 211 | | |
| | | 212 | | // HTTP clients. |
| | 0 | 213 | | services.AddHttpClient<IFileDownloader, HttpClientFileDownloader>(); |
| | | 214 | | |
| | | 215 | | // Add selectors. |
| | 0 | 216 | | foreach (var httpCorrelationIdSelectorType in HttpCorrelationIdSelectorTypes) |
| | 0 | 217 | | services.AddScoped(typeof(IHttpCorrelationIdSelector), httpCorrelationIdSelectorType); |
| | | 218 | | |
| | 0 | 219 | | foreach (var httpWorkflowInstanceIdSelectorType in HttpWorkflowInstanceIdSelectorTypes) |
| | 0 | 220 | | services.AddScoped(typeof(IHttpWorkflowInstanceIdSelector), httpWorkflowInstanceIdSelectorType); |
| | | 221 | | |
| | 0 | 222 | | services.Configure<ExpressionOptions>(options => |
| | 0 | 223 | | { |
| | 0 | 224 | | options.AddTypeAlias<HttpRequest>("HttpRequest"); |
| | 0 | 225 | | options.AddTypeAlias<HttpResponse>("HttpResponse"); |
| | 0 | 226 | | options.AddTypeAlias<HttpResponseMessage>("HttpResponseMessage"); |
| | 0 | 227 | | options.AddTypeAlias<HttpHeaders>("HttpHeaders"); |
| | 0 | 228 | | options.AddTypeAlias<HttpRouteData>("RouteData"); |
| | 0 | 229 | | options.AddTypeAlias<IFormFile>("FormFile"); |
| | 0 | 230 | | options.AddTypeAlias<IFormFile[]>("FormFile[]"); |
| | 0 | 231 | | options.AddTypeAlias<HttpFile>("HttpFile"); |
| | 0 | 232 | | options.AddTypeAlias<HttpFile[]>("HttpFile[]"); |
| | 0 | 233 | | options.AddTypeAlias<Downloadable>("Downloadable"); |
| | 0 | 234 | | options.AddTypeAlias<Downloadable[]>("Downloadable[]"); |
| | 0 | 235 | | }); |
| | 0 | 236 | | } |
| | | 237 | | |
| | | 238 | | /// <inheritdoc /> |
| | | 239 | | public void UseMiddleware(IApplicationBuilder app, IHostEnvironment? environment) |
| | | 240 | | { |
| | 0 | 241 | | app.UseWorkflows(); |
| | 0 | 242 | | } |
| | | 243 | | } |