| | | 1 | | using System.Net; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Http.ContentWriters; |
| | | 5 | | using Elsa.Http.UIHints; |
| | | 6 | | using Elsa.Workflows; |
| | | 7 | | using Elsa.Workflows.Attributes; |
| | | 8 | | using Elsa.Workflows.UIHints; |
| | | 9 | | using Elsa.Workflows.Exceptions; |
| | | 10 | | using Elsa.Workflows.Models; |
| | | 11 | | using Microsoft.AspNetCore.Http; |
| | | 12 | | using Microsoft.Extensions.Options; |
| | | 13 | | using Elsa.Http.Options; |
| | | 14 | | |
| | | 15 | | namespace Elsa.Http; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Write a response to the current HTTP response object. |
| | | 19 | | /// </summary> |
| | | 20 | | [Activity("Elsa", "HTTP", "Write a response to the current HTTP response object.", DisplayName = "HTTP Response")] |
| | | 21 | | public class WriteHttpResponse : Activity |
| | | 22 | | { |
| | | 23 | | /// <inheritdoc /> |
| | 52 | 24 | | public WriteHttpResponse([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, |
| | | 25 | | { |
| | 52 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | 0 | 29 | | public WriteHttpResponse(Input<object?> content, [CallerFilePath] string? source = null, [CallerLineNumber] int? lin |
| | | 30 | | { |
| | 0 | 31 | | Content = content; |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | 0 | 35 | | public WriteHttpResponse(Input<object?> content, Input<string?> contentType, [CallerFilePath] string? source = null, |
| | | 36 | | { |
| | 0 | 37 | | Content = content; |
| | 0 | 38 | | ContentType = contentType; |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// The status code to return. |
| | | 43 | | /// </summary> |
| | | 44 | | [Input( |
| | | 45 | | DefaultValue = HttpStatusCode.OK, |
| | | 46 | | Description = "The status code to return.", |
| | | 47 | | UIHint = InputUIHints.DropDown |
| | | 48 | | )] |
| | 198 | 49 | | public Input<HttpStatusCode> StatusCode { get; set; } = new(HttpStatusCode.OK); |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// The content to write back. |
| | | 53 | | /// </summary> |
| | | 54 | | [Input(Description = "The content to write back. String values will be sent as-is, while objects will be serialized |
| | 164 | 55 | | public Input<object?> Content { get; set; } = null!; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// The content type to use when returning the response. |
| | | 59 | | /// </summary> |
| | | 60 | | [Input( |
| | | 61 | | Description = "The content type to write when sending the response.", |
| | | 62 | | UIHandler = typeof(HttpContentTypeOptionsProvider), |
| | | 63 | | UIHint = InputUIHints.DropDown |
| | | 64 | | )] |
| | 149 | 65 | | public Input<string?> ContentType { get; set; } = null!; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// The headers to return along with the response. |
| | | 69 | | /// </summary> |
| | | 70 | | [Input( |
| | | 71 | | Description = "The headers to send along with the response.", |
| | | 72 | | UIHint = InputUIHints.JsonEditor, |
| | | 73 | | Category = "Advanced" |
| | | 74 | | )] |
| | 197 | 75 | | public Input<HttpHeaders?> ResponseHeaders { get; set; } = new(new HttpHeaders()); |
| | | 76 | | |
| | | 77 | | /// <inheritdoc /> |
| | | 78 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 79 | | { |
| | 20 | 80 | | var httpContextAccessor = context.GetRequiredService<IHttpContextAccessor>(); |
| | 20 | 81 | | var httpContext = httpContextAccessor.HttpContext; |
| | | 82 | | |
| | 20 | 83 | | if (httpContext == null) |
| | | 84 | | { |
| | | 85 | | // We're executing in a non-HTTP context (e.g. in a virtual actor). |
| | | 86 | | // Create a bookmark to allow the invoker to export the state and resume execution from there. |
| | 1 | 87 | | context.CreateBookmark(OnResumeAsync, BookmarkMetadata.HttpCrossBoundary); |
| | 1 | 88 | | return; |
| | | 89 | | } |
| | | 90 | | |
| | 19 | 91 | | await WriteResponseAsync(context, httpContext.Response); |
| | 20 | 92 | | } |
| | | 93 | | |
| | | 94 | | private async ValueTask OnResumeAsync(ActivityExecutionContext context) |
| | | 95 | | { |
| | 0 | 96 | | var httpContextAccessor = context.GetRequiredService<IHttpContextAccessor>(); |
| | 0 | 97 | | var httpContext = httpContextAccessor.HttpContext; |
| | | 98 | | |
| | 0 | 99 | | if (httpContext == null) |
| | | 100 | | { |
| | | 101 | | // We're not in an HTTP context, so let's fail. |
| | 0 | 102 | | throw new FaultException(HttpFaultCodes.NoHttpContext, HttpFaultCategories.Http, DefaultFaultTypes.System, " |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | await WriteResponseAsync(context, httpContext.Response); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | private async Task WriteResponseAsync(ActivityExecutionContext context, HttpResponse response) |
| | | 109 | | { |
| | | 110 | | // Set status code. |
| | 19 | 111 | | var statusCode = StatusCode.GetOrDefault(context, () => HttpStatusCode.OK); |
| | 19 | 112 | | response.StatusCode = (int)statusCode; |
| | | 113 | | |
| | | 114 | | // Add headers. |
| | 19 | 115 | | var headers = context.GetHeaders(ResponseHeaders); |
| | 50 | 116 | | foreach (var header in headers) |
| | 6 | 117 | | response.Headers[header.Key] = header.Value; |
| | | 118 | | |
| | | 119 | | // Get content and content type. |
| | 19 | 120 | | var content = context.Get(Content); |
| | | 121 | | |
| | 19 | 122 | | if (content != null) |
| | | 123 | | { |
| | 7 | 124 | | var contentType = ContentType.GetOrDefault(context); |
| | | 125 | | |
| | 7 | 126 | | if (string.IsNullOrWhiteSpace(contentType)) |
| | 3 | 127 | | contentType = DetermineContentType(content); |
| | | 128 | | |
| | 7 | 129 | | var factories = context.GetServices<IHttpContentFactory>(); |
| | 34 | 130 | | var factory = factories.FirstOrDefault(httpContentFactory => httpContentFactory.SupportedContentTypes.Any(c |
| | 7 | 131 | | var httpContent = factory.CreateHttpContent(content, contentType); |
| | | 132 | | |
| | | 133 | | // Set content type. |
| | 7 | 134 | | response.ContentType = httpContent.Headers.ContentType?.ToString() ?? contentType; |
| | | 135 | | |
| | | 136 | | // Write content. |
| | 7 | 137 | | if (statusCode != HttpStatusCode.NoContent) |
| | | 138 | | { |
| | | 139 | | try |
| | | 140 | | { |
| | 6 | 141 | | await httpContent.CopyToAsync(response.Body); |
| | 6 | 142 | | } |
| | | 143 | | catch (NotSupportedException) |
| | | 144 | | { |
| | | 145 | | // This can happen the Content property is a type that cannot be serialized or contains a type that |
| | 0 | 146 | | await response.WriteAsync("The response includes a type that cannot be serialized."); |
| | | 147 | | } |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | // Check if the configuration is set to flush immediatly the response to the caller. |
| | 19 | 152 | | var options = context.GetRequiredService<IOptions<HttpActivityOptions>>(); |
| | 19 | 153 | | if (options.Value.WriteHttpResponseSynchronously) |
| | 1 | 154 | | await response.CompleteAsync(); |
| | | 155 | | |
| | | 156 | | // Complete activity. |
| | 19 | 157 | | await context.CompleteActivityAsync(); |
| | 19 | 158 | | } |
| | | 159 | | |
| | 3 | 160 | | private string DetermineContentType(object? content) => content is byte[] or Stream |
| | 3 | 161 | | ? "application/octet-stream" |
| | 3 | 162 | | : content is string |
| | 3 | 163 | | ? "text/plain" |
| | 3 | 164 | | : "application/json"; |
| | | 165 | | } |