| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.Expressions.Models; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Http.Bookmarks; |
| | | 6 | | using Elsa.Http.Extensions; |
| | | 7 | | using Elsa.Http.UIHints; |
| | | 8 | | using Elsa.Workflows; |
| | | 9 | | using Elsa.Workflows.Attributes; |
| | | 10 | | using Elsa.Workflows.UIHints; |
| | | 11 | | using Elsa.Workflows.Models; |
| | | 12 | | using Microsoft.AspNetCore.Http; |
| | | 13 | | using Microsoft.AspNetCore.Routing; |
| | | 14 | | using Microsoft.Extensions.DependencyInjection; |
| | | 15 | | |
| | | 16 | | namespace Elsa.Http; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Wait for an inbound HTTP request that matches the specified path and methods. |
| | | 20 | | /// </summary> |
| | | 21 | | [Activity("Elsa", "HTTP", "Wait for an inbound HTTP request that matches the specified path and methods.", DisplayName = |
| | | 22 | | [Output(IsSerializable = false)] |
| | | 23 | | public class HttpEndpoint : Trigger<HttpRequest> |
| | | 24 | | { |
| | | 25 | | internal const string HttpContextInputKey = "HttpContext"; |
| | | 26 | | internal const string PathInputKey = "Path"; |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | 346 | 29 | | public HttpEndpoint([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line |
| | | 30 | | { |
| | 346 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | 0 | 34 | | public HttpEndpoint(Input<string> path, Input<string> method, [CallerFilePath] string? source = null, [CallerLineNum |
| | | 35 | | { |
| | 0 | 36 | | Path = path; |
| | 0 | 37 | | SupportedMethods = new(ObjectLiteral.From(new[] { method })); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | 0 | 41 | | public HttpEndpoint(Input<string> path, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) |
| | | 42 | | { |
| | 0 | 43 | | Path = path; |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// The path to associate with the workflow. |
| | | 48 | | /// </summary> |
| | | 49 | | [Input( |
| | | 50 | | Description = "The path to associate with the workflow.", |
| | | 51 | | UIHint = InputUIHints.SingleLine, |
| | | 52 | | UIHandler = typeof(HttpEndpointPathUIHandler) |
| | | 53 | | )] |
| | 1364 | 54 | | public Input<string> Path { get; set; } = null!; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// The HTTP methods to accept. |
| | | 58 | | /// </summary> |
| | | 59 | | [Input( |
| | | 60 | | Description = "The HTTP methods to accept.", |
| | | 61 | | Options = new[] { "GET", "POST", "PUT", "HEAD", "DELETE" }, |
| | | 62 | | UIHint = InputUIHints.CheckList)] |
| | 1710 | 63 | | public Input<ICollection<string>> SupportedMethods { get; set; } = new(ObjectLiteral.From(new[] { HttpMethods.Get }) |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Allow authenticated requests only. |
| | | 67 | | /// </summary> |
| | | 68 | | [Input(Description = "Allow authenticated requests only.", Category = "Security")] |
| | 1441 | 69 | | public Input<bool> Authorize { get; set; } = new(false); |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Provide a policy to evaluate. If the policy fails, the request is forbidden. |
| | | 73 | | /// </summary> |
| | | 74 | | [Input(Description = "Provide a policy to evaluate. If the policy fails, the request is forbidden.", Category = "Sec |
| | 1441 | 75 | | public Input<string?> Policy { get; set; } = new(default(string?)); |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// The maximum time allowed to process the request. |
| | | 79 | | /// </summary> |
| | | 80 | | [Input(Description = "The maximum time allowed to process the request.", Category = "Upload")] |
| | 1095 | 81 | | public Input<TimeSpan?> RequestTimeout { get; set; } = null!; |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// The maximum request size allowed in bytes. |
| | | 85 | | /// </summary> |
| | | 86 | | [Input(Description = "The maximum request size allowed in bytes.", Category = "Upload")] |
| | 1309 | 87 | | public Input<long?> RequestSizeLimit { get; set; } = null!; |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// The maximum request size allowed in bytes. |
| | | 91 | | /// </summary> |
| | | 92 | | [Input(Description = "The maximum file size allowed in bytes for an individual file.", Category = "Upload")] |
| | 1038 | 93 | | public Input<long?> FileSizeLimit { get; set; } = null!; |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// The allowed file extensions, |
| | | 97 | | /// </summary> |
| | | 98 | | [Input(Description = "Only file extensions in this list are allowed. Leave empty to allow all extensions", Category |
| | 1038 | 99 | | public Input<ICollection<string>> AllowedFileExtensions { get; set; } = null!; |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// The allowed file extensions, |
| | | 103 | | /// </summary> |
| | | 104 | | [Input(Description = "File extensions in this list are forbidden. Leave empty to not block any extension.", Category |
| | 1038 | 105 | | public Input<ICollection<string>> BlockedFileExtensions { get; set; } = null!; |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// The allowed file extensions, |
| | | 109 | | /// </summary> |
| | | 110 | | [Input(Description = "Only MIME types in this list are allowed. Leave empty to allow all types", Category = "Upload" |
| | 1038 | 111 | | public Input<ICollection<string>> AllowedMimeTypes { get; set; } = null!; |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// A value indicating whether to expose the "Request too large" outcome. |
| | | 115 | | /// </summary> |
| | | 116 | | [Input(Description = "A value indicating whether to expose the \"Request too large\" outcome.", Category = "Outcomes |
| | 583 | 117 | | public bool ExposeRequestTooLargeOutcome { get; set; } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// A value indicating whether to expose the "File too large" outcome. |
| | | 121 | | /// </summary> |
| | | 122 | | [Input(Description = "A value indicating whether to expose the \"File too large\" outcome.", Category = "Outcomes")] |
| | 583 | 123 | | public bool ExposeFileTooLargeOutcome { get; set; } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// A value indicating whether to expose the "Invalid file extension" outcome. |
| | | 127 | | /// </summary> |
| | | 128 | | [Input(Description = "A value indicating whether to expose the \"Invalid file extension\" outcome.", Category = "Out |
| | 583 | 129 | | public bool ExposeInvalidFileExtensionOutcome { get; set; } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// A value indicating whether to expose the "Invalid file MIME type" outcome. |
| | | 133 | | /// </summary> |
| | | 134 | | [Input(Description = "A value indicating whether to expose the \"Invalid file MIME type\" outcome.", Category = "Out |
| | 583 | 135 | | public bool ExposeInvalidFileMimeTypeOutcome { get; set; } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// The parsed request content, if any. |
| | | 139 | | /// </summary> |
| | | 140 | | [Output(Description = "The parsed request content, if any.")] |
| | 1197 | 141 | | public Output<object?> ParsedContent { get; set; } = null!; |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// The uploaded files, if any. |
| | | 145 | | /// </summary> |
| | | 146 | | [Output(Description = "The uploaded files, if any.", IsSerializable = false)] |
| | 761 | 147 | | public Output<IFormFile[]> Files { get; set; } = null!; |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// The first uploaded file, if any. |
| | | 151 | | /// </summary> |
| | | 152 | | [Output(Description = "The first uploaded file, if any.", IsSerializable = false)] |
| | 476 | 153 | | public Output<IFormFile?> File { get; set; } = null!; |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// The parsed route data, if any. |
| | | 157 | | /// </summary> |
| | | 158 | | [Output(Description = "The parsed route data, if any.")] |
| | 998 | 159 | | public Output<IDictionary<string, object>> RouteData { get; set; } = null!; |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// The querystring data, if any. |
| | | 163 | | /// </summary> |
| | | 164 | | [Output(Description = "The querystring data, if any.")] |
| | 981 | 165 | | public Output<IDictionary<string, object>> QueryStringData { get; set; } = null!; |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// The headers, if any. |
| | | 169 | | /// </summary> |
| | | 170 | | [Output(Description = "The headers, if any.")] |
| | 981 | 171 | | public Output<IDictionary<string, object>> Headers { get; set; } = null!; |
| | | 172 | | |
| | | 173 | | /// <inheritdoc /> |
| | | 174 | | protected override IEnumerable<object> GetTriggerPayloads(TriggerIndexingContext context) |
| | | 175 | | { |
| | 57 | 176 | | context.TriggerName = HttpStimulusNames.HttpEndpoint; |
| | 57 | 177 | | return GetBookmarkPayloads(context.ExpressionExecutionContext); |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <inheritdoc /> |
| | | 181 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 182 | | { |
| | 218 | 183 | | var path = Path.Get(context); |
| | 218 | 184 | | var methods = SupportedMethods.GetOrDefault(context) ?? new List<string> { HttpMethods.Get }; |
| | 218 | 185 | | await context.WaitForHttpRequestAsync(path, methods, OnResumeAsync); |
| | 218 | 186 | | } |
| | | 187 | | |
| | | 188 | | private async ValueTask OnResumeAsync(ActivityExecutionContext context) |
| | | 189 | | { |
| | 214 | 190 | | var httpContextAccessor = context.GetRequiredService<IHttpContextAccessor>(); |
| | 214 | 191 | | var httpContext = httpContextAccessor.HttpContext; |
| | | 192 | | |
| | 214 | 193 | | if (httpContext == null) |
| | | 194 | | { |
| | | 195 | | // We're executing in a non-HTTP context (e.g. in a virtual actor). |
| | | 196 | | // Create a bookmark to allow the invoker to export the state and resume execution from there. |
| | 0 | 197 | | context.CreateCrossBoundaryBookmark(); |
| | 0 | 198 | | return; |
| | | 199 | | } |
| | | 200 | | |
| | 214 | 201 | | await HandleRequestAsync(context); |
| | 214 | 202 | | } |
| | | 203 | | |
| | | 204 | | private async Task HandleRequestAsync(ActivityExecutionContext context) |
| | | 205 | | { |
| | 214 | 206 | | var httpContextAccessor = context.GetRequiredService<IHttpContextAccessor>(); |
| | 214 | 207 | | var httpContext = httpContextAccessor.HttpContext!; |
| | | 208 | | |
| | | 209 | | // Provide the received HTTP request as output. |
| | 214 | 210 | | var request = httpContext.Request; |
| | 214 | 211 | | context.Set(Result, request); |
| | | 212 | | |
| | | 213 | | // Read route data, if any. |
| | 214 | 214 | | var path = context.GetWorkflowInput<PathString>(PathInputKey); |
| | 214 | 215 | | var routeData = GetRouteData(httpContext, path); |
| | 216 | 216 | | var routeDictionary = routeData.Values.ToDictionary(route => route.Key, route => route.Value!); |
| | 214 | 217 | | var queryStringDictionary = httpContext.Request.Query.ToObjectDictionary(); |
| | 214 | 218 | | var headersDictionary = httpContext.Request.Headers.ToObjectDictionary(); |
| | | 219 | | |
| | 214 | 220 | | context.Set(RouteData, routeDictionary); |
| | 214 | 221 | | context.Set(QueryStringData, queryStringDictionary); |
| | 214 | 222 | | context.Set(Headers, headersDictionary); |
| | | 223 | | |
| | | 224 | | // Validate request size. |
| | 214 | 225 | | if (!ValidateRequestSize(context, httpContext)) |
| | | 226 | | { |
| | 0 | 227 | | await HandleRequestTooLargeAsync(context, httpContext); |
| | 0 | 228 | | return; |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | // Handle Form Fields |
| | 214 | 232 | | if (request.HasFormContentType) |
| | | 233 | | { |
| | 0 | 234 | | var formFields = request.Form.ToObjectDictionary(); |
| | | 235 | | |
| | 0 | 236 | | ParsedContent.Set(context, formFields); |
| | | 237 | | |
| | | 238 | | // Read files, if any. |
| | 0 | 239 | | var files = ReadFilesAsync(context, request); |
| | | 240 | | |
| | 0 | 241 | | if (files.Any()) |
| | | 242 | | { |
| | 0 | 243 | | if (!ValidateFileSizes(context, httpContext, files)) |
| | | 244 | | { |
| | 0 | 245 | | await HandleFileSizeTooLargeAsync(context, httpContext); |
| | 0 | 246 | | return; |
| | | 247 | | } |
| | | 248 | | |
| | 0 | 249 | | if (!ValidateFileExtensionWhitelist(context, httpContext, files)) |
| | | 250 | | { |
| | 0 | 251 | | await HandleInvalidFileExtensionWhitelistAsync(context, httpContext); |
| | 0 | 252 | | return; |
| | | 253 | | } |
| | | 254 | | |
| | 0 | 255 | | if (!ValidateFileExtensionBlacklist(context, httpContext, files)) |
| | | 256 | | { |
| | 0 | 257 | | await HandleInvalidFileExtensionBlacklistAsync(context, httpContext); |
| | 0 | 258 | | return; |
| | | 259 | | } |
| | | 260 | | |
| | 0 | 261 | | if (!ValidateFileMimeTypes(context, httpContext, files)) |
| | | 262 | | { |
| | 0 | 263 | | await HandleInvalidFileMimeTypesAsync(context, httpContext); |
| | 0 | 264 | | return; |
| | | 265 | | } |
| | | 266 | | |
| | 0 | 267 | | Files.Set(context, files.ToArray()); |
| | 0 | 268 | | File.Set(context, files.FirstOrDefault()); |
| | | 269 | | } |
| | | 270 | | } |
| | | 271 | | else |
| | | 272 | | { |
| | | 273 | | // Parse Non-Form content. |
| | | 274 | | try |
| | | 275 | | { |
| | 214 | 276 | | var content = await ParseContentAsync(context, request); |
| | 214 | 277 | | ParsedContent.Set(context, content); |
| | 214 | 278 | | } |
| | 0 | 279 | | catch (JsonException e) |
| | | 280 | | { |
| | 0 | 281 | | await HandleInvalidJsonPayloadAsync(context, httpContext, e); |
| | 0 | 282 | | throw; |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | // Complete. |
| | 214 | 288 | | await context.CompleteActivityAsync(); |
| | 214 | 289 | | } |
| | | 290 | | |
| | | 291 | | private IFormFileCollection ReadFilesAsync(ActivityExecutionContext context, HttpRequest request) |
| | | 292 | | { |
| | 0 | 293 | | return request.HasFormContentType ? request.Form.Files : new FormFileCollection(); |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | private bool ValidateRequestSize(ActivityExecutionContext context, HttpContext httpContext) |
| | | 297 | | { |
| | 214 | 298 | | var requestSizeLimit = RequestSizeLimit.GetOrDefault(context); |
| | | 299 | | |
| | 214 | 300 | | if (!requestSizeLimit.HasValue) |
| | 214 | 301 | | return true; |
| | | 302 | | |
| | 0 | 303 | | var requestSize = httpContext.Request.ContentLength ?? 0; |
| | 0 | 304 | | return requestSize <= requestSizeLimit; |
| | | 305 | | } |
| | | 306 | | |
| | | 307 | | private async Task HandleRequestTooLargeAsync(ActivityExecutionContext context, HttpContext httpContext) |
| | | 308 | | { |
| | 0 | 309 | | var exposeRequestTooLargeOutcome = ExposeRequestTooLargeOutcome; |
| | | 310 | | |
| | 0 | 311 | | if (exposeRequestTooLargeOutcome) |
| | | 312 | | { |
| | 0 | 313 | | await context.CompleteActivityWithOutcomesAsync("Request too large"); |
| | | 314 | | } |
| | | 315 | | else |
| | | 316 | | { |
| | 0 | 317 | | var response = httpContext.Response; |
| | 0 | 318 | | response.StatusCode = StatusCodes.Status413PayloadTooLarge; |
| | 0 | 319 | | await response.WriteAsJsonAsync(new |
| | 0 | 320 | | { |
| | 0 | 321 | | Message = $"The maximum request size allowed is {RequestSizeLimit.Get(context)} bytes." |
| | 0 | 322 | | }); |
| | 0 | 323 | | await response.Body.FlushAsync(); |
| | 0 | 324 | | } |
| | 0 | 325 | | } |
| | | 326 | | |
| | | 327 | | private bool ValidateFileSizes(ActivityExecutionContext context, HttpContext httpContext, IFormFileCollection files) |
| | | 328 | | { |
| | 0 | 329 | | var fileSizeLimit = FileSizeLimit.GetOrDefault(context); |
| | | 330 | | |
| | 0 | 331 | | if (!fileSizeLimit.HasValue) |
| | 0 | 332 | | return true; |
| | | 333 | | |
| | 0 | 334 | | if (!files.Any(file => file.Length > fileSizeLimit.Value)) |
| | 0 | 335 | | return true; |
| | | 336 | | |
| | 0 | 337 | | return false; |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | private async Task HandleFileSizeTooLargeAsync(ActivityExecutionContext context, HttpContext httpContext) |
| | | 341 | | { |
| | 0 | 342 | | var exposeFileTooLargeOutcome = ExposeFileTooLargeOutcome; |
| | | 343 | | |
| | 0 | 344 | | if (exposeFileTooLargeOutcome) |
| | | 345 | | { |
| | 0 | 346 | | await context.CompleteActivityWithOutcomesAsync("File too large"); |
| | | 347 | | } |
| | | 348 | | else |
| | | 349 | | { |
| | 0 | 350 | | var response = httpContext.Response; |
| | 0 | 351 | | response.StatusCode = StatusCodes.Status413PayloadTooLarge; |
| | 0 | 352 | | await response.WriteAsJsonAsync(new |
| | 0 | 353 | | { |
| | 0 | 354 | | Message = $"The maximum file size allowed is {FileSizeLimit.Get(context)} bytes." |
| | 0 | 355 | | }); |
| | 0 | 356 | | await response.Body.FlushAsync(); |
| | 0 | 357 | | } |
| | 0 | 358 | | } |
| | | 359 | | |
| | | 360 | | private bool ValidateFileExtensionWhitelist(ActivityExecutionContext context, HttpContext httpContext, IFormFileColl |
| | | 361 | | { |
| | 0 | 362 | | var allowedFileExtensions = AllowedFileExtensions.GetOrDefault(context); |
| | | 363 | | |
| | 0 | 364 | | if (allowedFileExtensions == null || !allowedFileExtensions.Any()) |
| | 0 | 365 | | return true; |
| | | 366 | | |
| | 0 | 367 | | if (files.All(file => allowedFileExtensions.Contains(System.IO.Path.GetExtension(file.FileName), StringComparer. |
| | 0 | 368 | | return true; |
| | | 369 | | |
| | 0 | 370 | | return false; |
| | | 371 | | } |
| | | 372 | | |
| | | 373 | | private async Task HandleInvalidFileExtensionWhitelistAsync(ActivityExecutionContext context, HttpContext httpContex |
| | | 374 | | { |
| | 0 | 375 | | if (ExposeInvalidFileExtensionOutcome) |
| | | 376 | | { |
| | 0 | 377 | | await context.CompleteActivityWithOutcomesAsync("Invalid file extension"); |
| | 0 | 378 | | return; |
| | | 379 | | } |
| | | 380 | | |
| | 0 | 381 | | var response = httpContext.Response; |
| | 0 | 382 | | var allowedFileExtensions = AllowedFileExtensions.GetOrDefault(context)!; |
| | 0 | 383 | | response.StatusCode = StatusCodes.Status415UnsupportedMediaType; |
| | 0 | 384 | | await response.WriteAsJsonAsync(new |
| | 0 | 385 | | { |
| | 0 | 386 | | Message = $"Only the following file extensions are allowed: {string.Join(", ", allowedFileExtensions)}" |
| | 0 | 387 | | }); |
| | 0 | 388 | | await response.Body.FlushAsync(); |
| | 0 | 389 | | } |
| | | 390 | | |
| | | 391 | | private bool ValidateFileExtensionBlacklist(ActivityExecutionContext context, HttpContext httpContext, IFormFileColl |
| | | 392 | | { |
| | 0 | 393 | | var blockedFileExtensions = BlockedFileExtensions.GetOrDefault(context); |
| | | 394 | | |
| | 0 | 395 | | if (blockedFileExtensions == null || !blockedFileExtensions.Any()) |
| | 0 | 396 | | return true; |
| | | 397 | | |
| | 0 | 398 | | if (!files.Any(file => blockedFileExtensions.Contains(System.IO.Path.GetExtension(file.FileName), StringComparer |
| | 0 | 399 | | return true; |
| | | 400 | | |
| | 0 | 401 | | return false; |
| | | 402 | | } |
| | | 403 | | |
| | | 404 | | private async Task HandleInvalidFileExtensionBlacklistAsync(ActivityExecutionContext context, HttpContext httpContex |
| | | 405 | | { |
| | 0 | 406 | | if (ExposeInvalidFileExtensionOutcome) |
| | | 407 | | { |
| | 0 | 408 | | await context.CompleteActivityWithOutcomesAsync("Invalid file extension"); |
| | 0 | 409 | | return; |
| | | 410 | | } |
| | | 411 | | |
| | 0 | 412 | | var blockedFileExtensions = BlockedFileExtensions.GetOrDefault(context)!; |
| | 0 | 413 | | var response = httpContext.Response; |
| | 0 | 414 | | response.StatusCode = StatusCodes.Status415UnsupportedMediaType; |
| | 0 | 415 | | await response.WriteAsJsonAsync(new |
| | 0 | 416 | | { |
| | 0 | 417 | | Message = $"The following file extensions are not allowed: {string.Join(", ", blockedFileExtensions)}" |
| | 0 | 418 | | }); |
| | 0 | 419 | | await response.Body.FlushAsync(); |
| | 0 | 420 | | } |
| | | 421 | | |
| | | 422 | | private bool ValidateFileMimeTypes(ActivityExecutionContext context, HttpContext httpContext, IFormFileCollection fi |
| | | 423 | | { |
| | 0 | 424 | | var allowedMimeTypes = AllowedMimeTypes.GetOrDefault(context); |
| | | 425 | | |
| | 0 | 426 | | if (allowedMimeTypes == null || !allowedMimeTypes.Any()) |
| | 0 | 427 | | return true; |
| | | 428 | | |
| | 0 | 429 | | if (files.All(file => allowedMimeTypes.Contains(file.ContentType, StringComparer.OrdinalIgnoreCase))) |
| | 0 | 430 | | return true; |
| | | 431 | | |
| | 0 | 432 | | return false; |
| | | 433 | | } |
| | | 434 | | |
| | | 435 | | private async Task HandleInvalidFileMimeTypesAsync(ActivityExecutionContext context, HttpContext httpContext) |
| | | 436 | | { |
| | 0 | 437 | | if (ExposeInvalidFileMimeTypeOutcome) |
| | | 438 | | { |
| | 0 | 439 | | await context.CompleteActivityWithOutcomesAsync("Invalid file MIME type"); |
| | 0 | 440 | | return; |
| | | 441 | | } |
| | | 442 | | |
| | 0 | 443 | | var allowedMimeTypes = AllowedMimeTypes.GetOrDefault(context)!; |
| | 0 | 444 | | var response = httpContext.Response; |
| | 0 | 445 | | response.StatusCode = StatusCodes.Status415UnsupportedMediaType; |
| | 0 | 446 | | await response.WriteAsJsonAsync(new |
| | 0 | 447 | | { |
| | 0 | 448 | | Message = $"Only the following MIME types are allowed: {string.Join(", ", allowedMimeTypes)}" |
| | 0 | 449 | | }); |
| | 0 | 450 | | await response.Body.FlushAsync(); |
| | 0 | 451 | | } |
| | | 452 | | |
| | | 453 | | private async Task HandleInvalidJsonPayloadAsync(ActivityExecutionContext context, HttpContext httpContext, JsonExce |
| | | 454 | | { |
| | 0 | 455 | | var response = httpContext.Response; |
| | 0 | 456 | | response.StatusCode = StatusCodes.Status400BadRequest; |
| | 0 | 457 | | await response.WriteAsJsonAsync(new |
| | 0 | 458 | | { |
| | 0 | 459 | | exception.Message, |
| | 0 | 460 | | exception.Path, |
| | 0 | 461 | | exception.LineNumber, |
| | 0 | 462 | | }); |
| | 0 | 463 | | await response.Body.FlushAsync(); |
| | 0 | 464 | | } |
| | | 465 | | |
| | | 466 | | private async Task<object?> ParseContentAsync(ActivityExecutionContext context, HttpRequest httpRequest) |
| | | 467 | | { |
| | 214 | 468 | | if (!HasContent(httpRequest)) |
| | 14 | 469 | | return null; |
| | | 470 | | |
| | 200 | 471 | | var cancellationToken = context.CancellationToken; |
| | 200 | 472 | | var targetType = ParsedContent.GetTargetType(context); |
| | 200 | 473 | | var contentStream = httpRequest.Body; |
| | 200 | 474 | | var contentType = httpRequest.ContentType!; |
| | 1800 | 475 | | var headers = httpRequest.Headers.ToDictionary(x => x.Key, x => x.Value.ToArray()); |
| | | 476 | | |
| | 200 | 477 | | return await context.ParseContentAsync(contentStream, contentType, targetType, headers!, cancellationToken); |
| | 214 | 478 | | } |
| | | 479 | | |
| | 214 | 480 | | private static bool HasContent(HttpRequest httpRequest) => httpRequest.Headers.ContentLength > 0; |
| | | 481 | | |
| | | 482 | | private IEnumerable<object> GetBookmarkPayloads(ExpressionExecutionContext context) |
| | | 483 | | { |
| | | 484 | | // Generate bookmark data for path and selected methods. |
| | 57 | 485 | | var normalizedRoute = context.Get(Path)!.NormalizeRoute(); |
| | 57 | 486 | | var methods = SupportedMethods.GetOrDefault(context) ?? new List<string> { HttpMethods.Get }; |
| | 57 | 487 | | var authorize = Authorize.GetOrDefault(context); |
| | 57 | 488 | | var policy = Policy.GetOrDefault(context); |
| | 57 | 489 | | var requestTimeout = RequestTimeout.GetOrDefault(context); |
| | 57 | 490 | | var requestSizeLimit = RequestSizeLimit.GetOrDefault(context); |
| | | 491 | | |
| | 57 | 492 | | return methods |
| | 57 | 493 | | .Select(x => new HttpEndpointBookmarkPayload(normalizedRoute, x.ToLowerInvariant(), authorize, policy, reque |
| | 57 | 494 | | .Cast<object>() |
| | 57 | 495 | | .ToArray(); |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | private static RouteData GetRouteData(HttpContext httpContext, string path) |
| | | 499 | | { |
| | 214 | 500 | | var routeData = httpContext.GetRouteData(); |
| | 214 | 501 | | var routeTable = httpContext.RequestServices.GetRequiredService<IRouteTable>(); |
| | 214 | 502 | | var routeMatcher = httpContext.RequestServices.GetRequiredService<IRouteMatcher>(); |
| | | 503 | | |
| | 214 | 504 | | var matchingRouteQuery = |
| | 214 | 505 | | from route in routeTable |
| | 680 | 506 | | let routeValues = routeMatcher.Match(route.Route, path) |
| | 680 | 507 | | where routeValues != null |
| | 428 | 508 | | select new { route, routeValues }; |
| | | 509 | | |
| | 214 | 510 | | var matchingRoute = matchingRouteQuery.FirstOrDefault(); |
| | | 511 | | |
| | 214 | 512 | | if (matchingRoute == null) |
| | 0 | 513 | | return routeData; |
| | | 514 | | |
| | 430 | 515 | | foreach (var (key, value) in matchingRoute.routeValues!) |
| | 1 | 516 | | routeData.Values[key] = value; |
| | | 517 | | |
| | 214 | 518 | | return routeData; |
| | | 519 | | } |
| | | 520 | | } |