| | | 1 | | using System.Net; |
| | | 2 | | using System.Net.Http.Headers; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Http.ContentWriters; |
| | | 5 | | using Elsa.Http.UIHints; |
| | | 6 | | using Elsa.Resilience; |
| | | 7 | | using Elsa.Resilience.Models; |
| | | 8 | | using Elsa.Workflows; |
| | | 9 | | using Elsa.Workflows.Attributes; |
| | | 10 | | using Elsa.Workflows.UIHints; |
| | | 11 | | using Elsa.Workflows.Models; |
| | | 12 | | using Microsoft.Extensions.Logging; |
| | | 13 | | using Polly; |
| | | 14 | | |
| | | 15 | | namespace Elsa.Http; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Base class for activities that send HTTP requests. |
| | | 19 | | /// </summary> |
| | | 20 | | [Output(IsSerializable = false)] |
| | | 21 | | [ResilienceCategory("HTTP")] |
| | 83 | 22 | | public abstract class SendHttpRequestBase(string? source = null, int? line = null) : Activity<HttpResponseMessage>(sourc |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// The URL to send the request to. |
| | | 26 | | /// </summary> |
| | 106 | 27 | | [Input(Order = 0)] public Input<Uri?> Url { get; set; } = null!; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// The HTTP method to use when sending the request. |
| | | 31 | | /// </summary> |
| | | 32 | | [Input( |
| | | 33 | | Description = "The HTTP method to use when sending the request.", |
| | | 34 | | Options = new[] |
| | | 35 | | { |
| | | 36 | | "GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD" |
| | | 37 | | }, |
| | | 38 | | DefaultValue = "GET", |
| | | 39 | | UIHint = InputUIHints.DropDown, |
| | | 40 | | Order = 1 |
| | | 41 | | )] |
| | 189 | 42 | | public Input<string> Method { get; set; } = new("GET"); |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// The content to send with the request. Can be a string, an object, a byte array or a stream. |
| | | 46 | | /// </summary> |
| | | 47 | | [Input( |
| | | 48 | | Description = "The content to send with the request. Can be a string, an object, a byte array or a stream.", |
| | | 49 | | Order = 2 |
| | | 50 | | )] |
| | 100 | 51 | | public Input<object?> Content { get; set; } = null!; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// The content type to use when sending the request. |
| | | 55 | | /// </summary> |
| | | 56 | | [Input( |
| | | 57 | | Description = "The content type to use when sending the request.", |
| | | 58 | | UIHandler = typeof(HttpContentTypeOptionsProvider), |
| | | 59 | | UIHint = InputUIHints.DropDown, |
| | | 60 | | Order = 3 |
| | | 61 | | )] |
| | 100 | 62 | | public Input<string?> ContentType { get; set; } = null!; |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// The Authorization header value to send with the request. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <example>Bearer {some-access-token}</example> |
| | | 68 | | [Input( |
| | | 69 | | Description = "The Authorization header value to send with the request. For example: Bearer {some-access-token}" |
| | | 70 | | Category = "Security", |
| | | 71 | | CanContainSecrets = true, |
| | | 72 | | Order = 4 |
| | | 73 | | )] |
| | 100 | 74 | | public Input<string?> Authorization { get; set; } = null!; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// A value that allows to add the Authorization header without validation. |
| | | 78 | | /// </summary> |
| | | 79 | | [Input( |
| | | 80 | | Description = "A value that allows to add the Authorization header without validation.", |
| | | 81 | | Category = "Security", |
| | | 82 | | Order = 5 |
| | | 83 | | )] |
| | 80 | 84 | | public Input<bool> DisableAuthorizationHeaderValidation { get; set; } = null!; |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// The headers to send along with the request. |
| | | 88 | | /// </summary> |
| | | 89 | | [Input( |
| | | 90 | | Description = "The headers to send along with the request.", |
| | | 91 | | UIHint = InputUIHints.JsonEditor, |
| | | 92 | | Category = "Advanced", |
| | | 93 | | Order = 6 |
| | | 94 | | )] |
| | 163 | 95 | | public Input<HttpHeaders?> RequestHeaders { get; set; } = new(new HttpHeaders()); |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Indicates whether resiliency mechanisms should be enabled for the HTTP request. |
| | | 99 | | /// </summary> |
| | | 100 | | [Obsolete("Use the common Resilience Strategy setting instead.")] |
| | | 101 | | [Input(Description = "Obsolete. Use the common Resilience Strategy setting instead.")] |
| | 78 | 102 | | public Input<bool> EnableResiliency { get; set; } = null!; |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// The HTTP response status code |
| | | 106 | | /// </summary> |
| | | 107 | | [Output(Description = "The HTTP response status code")] |
| | 48 | 108 | | public Output<int> StatusCode { get; set; } = null!; |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// The parsed content, if any. |
| | | 112 | | /// </summary> |
| | | 113 | | [Output(Description = "The parsed content, if any.")] |
| | 55 | 114 | | public Output<object?> ParsedContent { get; set; } = null!; |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// The response headers that were received. |
| | | 118 | | /// </summary> |
| | | 119 | | [Output(Description = "The response headers that were received.")] |
| | 48 | 120 | | public Output<HttpHeaders?> ResponseHeaders { get; set; } = null!; |
| | | 121 | | |
| | | 122 | | /// <inheritdoc /> |
| | | 123 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 124 | | { |
| | 26 | 125 | | await TrySendAsync(context); |
| | 26 | 126 | | } |
| | | 127 | | |
| | | 128 | | public IDictionary<string, string?> CollectRetryDetails(ActivityExecutionContext context, RetryAttempt attempt) |
| | | 129 | | { |
| | 2 | 130 | | if (attempt.Result is not HttpResponseMessage response) |
| | 0 | 131 | | return new Dictionary<string, string?>(); |
| | | 132 | | |
| | 2 | 133 | | return new Dictionary<string, string?> |
| | 2 | 134 | | { |
| | 2 | 135 | | ["StatusCode"] = response.StatusCode.ToString(), |
| | 2 | 136 | | ["ReasonPhrase"] = response.ReasonPhrase, |
| | 2 | 137 | | ["Content-Type"] = response.Content.Headers.ContentType?.MediaType ?? "application/octet-stream", |
| | 2 | 138 | | ["Date"] = response.Headers.Date.ToString(), |
| | 2 | 139 | | ["Retry-After"] = response.Headers.RetryAfter?.ToString() |
| | 2 | 140 | | }; |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Handles the response. |
| | | 145 | | /// </summary> |
| | | 146 | | protected abstract ValueTask HandleResponseAsync(ActivityExecutionContext context, HttpResponseMessage response); |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Handles an exception that occurred while sending the request. |
| | | 150 | | /// </summary> |
| | | 151 | | protected abstract ValueTask HandleRequestExceptionAsync(ActivityExecutionContext context, HttpRequestException exce |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Handles <see cref="TaskCanceledException"/> that occurred while sending the request. |
| | | 155 | | /// </summary> |
| | | 156 | | protected abstract ValueTask HandleTaskCanceledExceptionAsync(ActivityExecutionContext context, TaskCanceledExceptio |
| | | 157 | | |
| | | 158 | | private async Task TrySendAsync(ActivityExecutionContext context) |
| | | 159 | | { |
| | 26 | 160 | | var logger = (ILogger)context.GetRequiredService(typeof(ILogger<>).MakeGenericType(GetType())); |
| | 26 | 161 | | var httpClientFactory = context.GetRequiredService<IHttpClientFactory>(); |
| | 26 | 162 | | var httpClient = httpClientFactory.CreateClient(nameof(SendHttpRequestBase)); |
| | 26 | 163 | | var cancellationToken = context.CancellationToken; |
| | 26 | 164 | | var resiliencyEnabled = EnableResiliency.GetOrDefault(context, () => false); |
| | | 165 | | |
| | | 166 | | try |
| | | 167 | | { |
| | 26 | 168 | | var response = await SendRequestAsync(context); |
| | 22 | 169 | | var parsedContent = await ParseContentAsync(context, response); |
| | 22 | 170 | | var statusCode = (int)response.StatusCode; |
| | 22 | 171 | | var responseHeaders = new HttpHeaders(response.Headers); |
| | | 172 | | |
| | 22 | 173 | | context.Set(Result, response); |
| | 22 | 174 | | context.Set(ParsedContent, parsedContent); |
| | 22 | 175 | | context.Set(StatusCode, statusCode); |
| | 22 | 176 | | context.Set(ResponseHeaders, responseHeaders); |
| | | 177 | | |
| | 22 | 178 | | await HandleResponseAsync(context, response); |
| | 22 | 179 | | } |
| | 2 | 180 | | catch (HttpRequestException e) |
| | | 181 | | { |
| | 2 | 182 | | logger.LogWarning(e, "An error occurred while sending an HTTP request"); |
| | 2 | 183 | | context.AddExecutionLogEntry("Error", e.Message, payload: new |
| | 2 | 184 | | { |
| | 2 | 185 | | e.StackTrace |
| | 2 | 186 | | }); |
| | 2 | 187 | | context.JournalData.Add("Error", e.Message); |
| | 2 | 188 | | await HandleRequestExceptionAsync(context, e); |
| | 2 | 189 | | } |
| | 2 | 190 | | catch (TaskCanceledException e) |
| | | 191 | | { |
| | 2 | 192 | | logger.LogWarning(e, "An error occurred while sending an HTTP request"); |
| | 2 | 193 | | context.AddExecutionLogEntry("Error", e.Message, payload: new |
| | 2 | 194 | | { |
| | 2 | 195 | | e.StackTrace |
| | 2 | 196 | | }); |
| | 2 | 197 | | context.JournalData.Add("Cancelled", true); |
| | 2 | 198 | | await HandleTaskCanceledExceptionAsync(context, e); |
| | | 199 | | } |
| | | 200 | | |
| | 26 | 201 | | return; |
| | | 202 | | |
| | | 203 | | async Task<HttpResponseMessage> SendRequestAsync(ActivityExecutionContext activityExecutionContext) |
| | | 204 | | { |
| | | 205 | | // Keep this for backward compatibility. |
| | 26 | 206 | | if (resiliencyEnabled) |
| | | 207 | | { |
| | 0 | 208 | | var pipeline = BuildResiliencyPipeline(context); |
| | 0 | 209 | | return await pipeline.ExecuteAsync(async ct => await SendRequestAsyncCore(ct), cancellationToken); |
| | | 210 | | } |
| | | 211 | | |
| | 26 | 212 | | var resilienceService = activityExecutionContext.GetRequiredService<IResilientActivityInvoker>(); |
| | 54 | 213 | | return await resilienceService.InvokeAsync(this, activityExecutionContext, async () => await SendRequestAsyn |
| | 22 | 214 | | } |
| | | 215 | | |
| | | 216 | | async Task<HttpResponseMessage> SendRequestAsyncCore(CancellationToken ct = default) |
| | | 217 | | { |
| | 28 | 218 | | var request = PrepareRequest(context); |
| | | 219 | | |
| | 28 | 220 | | return await httpClient.SendAsync(request, ct); |
| | 24 | 221 | | } |
| | 26 | 222 | | } |
| | | 223 | | |
| | | 224 | | private async Task<object?> ParseContentAsync(ActivityExecutionContext context, HttpResponseMessage httpResponse) |
| | | 225 | | { |
| | 22 | 226 | | var httpContent = httpResponse.Content; |
| | 22 | 227 | | if (!HasContent(httpContent)) |
| | 16 | 228 | | return null; |
| | | 229 | | |
| | 6 | 230 | | var cancellationToken = context.CancellationToken; |
| | 6 | 231 | | var targetType = ParsedContent.GetTargetType(context); |
| | 6 | 232 | | var contentStream = await httpContent.ReadAsStreamAsync(cancellationToken); |
| | 6 | 233 | | var responseHeaders = httpResponse.Headers; |
| | 6 | 234 | | var contentHeaders = httpContent.Headers; |
| | 6 | 235 | | var contentType = contentHeaders.ContentType?.MediaType ?? "application/octet-stream"; |
| | | 236 | | |
| | 6 | 237 | | targetType ??= contentType switch |
| | 6 | 238 | | { |
| | 6 | 239 | | "application/json" => typeof(object), |
| | 0 | 240 | | _ => typeof(string) |
| | 6 | 241 | | }; |
| | | 242 | | |
| | 30 | 243 | | var contentHeadersDictionary = contentHeaders.ToDictionary(x => x.Key, x => x.Value.ToArray(), StringComparer.Or |
| | 6 | 244 | | var responseHeadersDictionary = responseHeaders.ToDictionary(x => x.Key, x => x.Value.ToArray(), StringComparer. |
| | 30 | 245 | | var headersDictionary = contentHeadersDictionary.Concat(responseHeadersDictionary).ToDictionary(x => x.Key, x => |
| | 6 | 246 | | return await context.ParseContentAsync(contentStream, contentType, targetType, headersDictionary, cancellationTo |
| | 22 | 247 | | } |
| | | 248 | | |
| | 22 | 249 | | private static bool HasContent(HttpContent httpContent) => httpContent.Headers.ContentLength > 0; |
| | | 250 | | |
| | | 251 | | private HttpRequestMessage PrepareRequest(ActivityExecutionContext context) |
| | | 252 | | { |
| | 28 | 253 | | var method = Method.GetOrDefault(context) ?? "GET"; |
| | 28 | 254 | | var url = Url.Get(context); |
| | 28 | 255 | | var request = new HttpRequestMessage(new HttpMethod(method), url); |
| | 28 | 256 | | var headers = context.GetHeaders(RequestHeaders); |
| | 28 | 257 | | var authorization = Authorization.GetOrDefault(context); |
| | 28 | 258 | | var addAuthorizationWithoutValidation = DisableAuthorizationHeaderValidation.GetOrDefault(context); |
| | | 259 | | |
| | 28 | 260 | | if (!string.IsNullOrWhiteSpace(authorization)) |
| | 6 | 261 | | if (addAuthorizationWithoutValidation) |
| | 0 | 262 | | request.Headers.TryAddWithoutValidation("Authorization", authorization); |
| | | 263 | | else |
| | 6 | 264 | | request.Headers.Authorization = AuthenticationHeaderValue.Parse(authorization); |
| | | 265 | | |
| | 56 | 266 | | foreach (var header in headers) |
| | 0 | 267 | | request.Headers.Add(header.Key, header.Value.AsEnumerable()); |
| | | 268 | | |
| | 28 | 269 | | var contentType = ContentType.GetOrDefault(context); |
| | 28 | 270 | | var content = Content.GetOrDefault(context); |
| | | 271 | | |
| | 28 | 272 | | if (contentType != null && content != null) |
| | | 273 | | { |
| | 0 | 274 | | var factories = context.GetServices<IHttpContentFactory>(); |
| | 0 | 275 | | var factory = SelectContentWriter(contentType, factories); |
| | 0 | 276 | | request.Content = factory.CreateHttpContent(content, contentType); |
| | | 277 | | } |
| | | 278 | | |
| | 28 | 279 | | return request; |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | private IHttpContentFactory SelectContentWriter(string? contentType, IEnumerable<IHttpContentFactory> factories) |
| | | 283 | | { |
| | 0 | 284 | | if (string.IsNullOrWhiteSpace(contentType)) |
| | 0 | 285 | | return new JsonContentFactory(); |
| | | 286 | | |
| | 0 | 287 | | var parsedContentType = new System.Net.Mime.ContentType(contentType); |
| | 0 | 288 | | return factories.FirstOrDefault(httpContentFactory => httpContentFactory.SupportedContentTypes.Any(c => c == par |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | private ResiliencePipeline<HttpResponseMessage> BuildResiliencyPipeline(ActivityExecutionContext context) |
| | | 292 | | { |
| | | 293 | | // Docs: https://www.pollydocs.org/strategies/retry |
| | 0 | 294 | | var pipelineBuilder = new ResiliencePipelineBuilder<HttpResponseMessage>() |
| | 0 | 295 | | .AddRetry(new() |
| | 0 | 296 | | { |
| | 0 | 297 | | ShouldHandle = new PredicateBuilder<HttpResponseMessage>() |
| | 0 | 298 | | .Handle<TimeoutException>() // Specific timeout exception |
| | 0 | 299 | | .Handle<HttpRequestException>() // Any HTTP exception |
| | 0 | 300 | | .HandleResult(response => IsTransientStatusCode(response.StatusCode)), |
| | 0 | 301 | | MaxRetryAttempts = 8, |
| | 0 | 302 | | UseJitter = false, // If enabled, adds a random value between -25% and +25% of the calculated Delay, exc |
| | 0 | 303 | | Delay = TimeSpan.FromSeconds(1), |
| | 0 | 304 | | BackoffType = DelayBackoffType.Exponential // Delay * 2^AttemptNumber, e.g. [ 2s, 4s, 8s, 16s ]. Total s |
| | 0 | 305 | | // If BackoffType is Exponential, then the calculated Delay is multiplied by a random value between -25% |
| | 0 | 306 | | }); |
| | | 307 | | |
| | 0 | 308 | | return pipelineBuilder.Build(); |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | // Helper method to identify transient status codes. |
| | | 312 | | private static bool IsTransientStatusCode(HttpStatusCode? statusCode) |
| | | 313 | | { |
| | 0 | 314 | | if (statusCode is null) |
| | | 315 | | { |
| | | 316 | | // No status code -> Assume network failure, worth retrying. |
| | 0 | 317 | | return true; |
| | | 318 | | } |
| | | 319 | | |
| | 0 | 320 | | return statusCode.Value switch |
| | 0 | 321 | | { |
| | 0 | 322 | | HttpStatusCode.RequestTimeout => true, // 408 |
| | 0 | 323 | | HttpStatusCode.TooManyRequests => true, // 429 (if no Retry-After header is respected) |
| | 0 | 324 | | HttpStatusCode.InternalServerError => true, // 500 |
| | 0 | 325 | | HttpStatusCode.BadGateway => true, // 502 |
| | 0 | 326 | | HttpStatusCode.ServiceUnavailable => true, // 503 |
| | 0 | 327 | | HttpStatusCode.GatewayTimeout => true, // 504 |
| | 0 | 328 | | HttpStatusCode.Conflict => true, // 409 - Can be transient in concurrency cases |
| | 0 | 329 | | _ => false // Other errors are not transient |
| | 0 | 330 | | }; |
| | | 331 | | } |
| | | 332 | | } |