|  | 
|  | 1 | +using System.Net; | 
|  | 2 | +using System.Text; | 
|  | 3 | +using System.Text.Json; | 
|  | 4 | +using JetBrains.Annotations; | 
|  | 5 | +using JsonApiDotNetCore.AtomicOperations; | 
|  | 6 | +using JsonApiDotNetCore.Configuration; | 
|  | 7 | +using JsonApiDotNetCore.Errors; | 
|  | 8 | +using JsonApiDotNetCore.Serialization.Objects; | 
|  | 9 | +using JsonApiDotNetCore.Serialization.Response; | 
|  | 10 | +using Microsoft.AspNetCore.Http; | 
|  | 11 | +using Microsoft.AspNetCore.Http.Extensions; | 
|  | 12 | +using Microsoft.AspNetCore.WebUtilities; | 
|  | 13 | +using Microsoft.IO; | 
|  | 14 | +using Microsoft.Net.Http.Headers; | 
|  | 15 | +using SysNotNull = System.Diagnostics.CodeAnalysis.NotNullAttribute; | 
|  | 16 | + | 
|  | 17 | +namespace JsonApiDotNetCore.Middleware; | 
|  | 18 | + | 
|  | 19 | +// IMPORTANT: In your Program.cs, make sure app.UseDeveloperExceptionPage() is called BEFORE this! | 
|  | 20 | + | 
|  | 21 | +public sealed class IdempotencyMiddleware | 
|  | 22 | +{ | 
|  | 23 | +    private static readonly RecyclableMemoryStreamManager MemoryStreamManager = new(); | 
|  | 24 | + | 
|  | 25 | +    private readonly IJsonApiOptions _options; | 
|  | 26 | +    private readonly IFingerprintGenerator _fingerprintGenerator; | 
|  | 27 | +    private readonly RequestDelegate _next; | 
|  | 28 | + | 
|  | 29 | +    public IdempotencyMiddleware(IJsonApiOptions options, IFingerprintGenerator fingerprintGenerator, RequestDelegate next) | 
|  | 30 | +    { | 
|  | 31 | +        ArgumentGuard.NotNull(options, nameof(options)); | 
|  | 32 | +        ArgumentGuard.NotNull(fingerprintGenerator, nameof(fingerprintGenerator)); | 
|  | 33 | + | 
|  | 34 | +        _options = options; | 
|  | 35 | +        _fingerprintGenerator = fingerprintGenerator; | 
|  | 36 | +        _next = next; | 
|  | 37 | +    } | 
|  | 38 | + | 
|  | 39 | +    public async Task InvokeAsync(HttpContext httpContext, IIdempotencyProvider idempotencyProvider) | 
|  | 40 | +    { | 
|  | 41 | +        try | 
|  | 42 | +        { | 
|  | 43 | +            await InnerInvokeAsync(httpContext, idempotencyProvider); | 
|  | 44 | +        } | 
|  | 45 | +        catch (JsonApiException exception) | 
|  | 46 | +        { | 
|  | 47 | +            await FlushResponseAsync(httpContext.Response, _options.SerializerWriteOptions, exception.Errors.Single()); | 
|  | 48 | +        } | 
|  | 49 | +    } | 
|  | 50 | + | 
|  | 51 | +    public async Task InnerInvokeAsync(HttpContext httpContext, IIdempotencyProvider idempotencyProvider) | 
|  | 52 | +    { | 
|  | 53 | +        string? idempotencyKey = GetIdempotencyKey(httpContext.Request.Headers); | 
|  | 54 | + | 
|  | 55 | +        if (idempotencyKey != null && idempotencyProvider is NoIdempotencyProvider) | 
|  | 56 | +        { | 
|  | 57 | +            throw new JsonApiException(new ErrorObject(HttpStatusCode.BadRequest) | 
|  | 58 | +            { | 
|  | 59 | +                Title = $"Invalid '{HeaderConstants.IdempotencyKey}' HTTP header.", | 
|  | 60 | +                Detail = "Idempotency is currently disabled.", | 
|  | 61 | +                Source = new ErrorSource | 
|  | 62 | +                { | 
|  | 63 | +                    Header = HeaderConstants.IdempotencyKey | 
|  | 64 | +                } | 
|  | 65 | +            }); | 
|  | 66 | +        } | 
|  | 67 | + | 
|  | 68 | +        if (!idempotencyProvider.IsSupported(httpContext.Request)) | 
|  | 69 | +        { | 
|  | 70 | +            await _next(httpContext); | 
|  | 71 | +            return; | 
|  | 72 | +        } | 
|  | 73 | + | 
|  | 74 | +        AssertIdempotencyKeyIsValid(idempotencyKey); | 
|  | 75 | + | 
|  | 76 | +        await BufferRequestBodyAsync(httpContext); | 
|  | 77 | + | 
|  | 78 | +        string requestFingerprint = await GetRequestFingerprintAsync(httpContext); | 
|  | 79 | +        IdempotentResponse? idempotentResponse = await idempotencyProvider.GetResponseFromCacheAsync(idempotencyKey, httpContext.RequestAborted); | 
|  | 80 | + | 
|  | 81 | +        if (idempotentResponse != null) | 
|  | 82 | +        { | 
|  | 83 | +            if (idempotentResponse.RequestFingerprint != requestFingerprint) | 
|  | 84 | +            { | 
|  | 85 | +                throw new JsonApiException(new ErrorObject(HttpStatusCode.UnprocessableEntity) | 
|  | 86 | +                { | 
|  | 87 | +                    Title = $"Invalid '{HeaderConstants.IdempotencyKey}' HTTP header.", | 
|  | 88 | +                    Detail = $"The provided idempotency key '{idempotencyKey}' is in use for another request.", | 
|  | 89 | +                    Source = new ErrorSource | 
|  | 90 | +                    { | 
|  | 91 | +                        Header = HeaderConstants.IdempotencyKey | 
|  | 92 | +                    } | 
|  | 93 | +                }); | 
|  | 94 | +            } | 
|  | 95 | + | 
|  | 96 | +            httpContext.Response.StatusCode = (int)idempotentResponse.ResponseStatusCode; | 
|  | 97 | +            httpContext.Response.Headers[HeaderConstants.IdempotencyKey] = $"\"{idempotencyKey}\""; | 
|  | 98 | +            httpContext.Response.Headers[HeaderNames.Location] = idempotentResponse.ResponseLocationHeader; | 
|  | 99 | + | 
|  | 100 | +            if (idempotentResponse.ResponseContentTypeHeader != null) | 
|  | 101 | +            { | 
|  | 102 | +                // Workaround for invalid nullability annotation in HttpResponse.ContentType | 
|  | 103 | +                // Fixed after ASP.NET 6 release, see https://github.com/dotnet/aspnetcore/commit/8bb128185b58a26065d0f29e695a2410cf0a3c68#diff-bbfd771a8ef013a9921bff36df0d69f424910e079945992f1dccb24de54ca717 | 
|  | 104 | +                httpContext.Response.ContentType = idempotentResponse.ResponseContentTypeHeader; | 
|  | 105 | +            } | 
|  | 106 | + | 
|  | 107 | +            await using TextWriter writer = new HttpResponseStreamWriter(httpContext.Response.Body, Encoding.UTF8); | 
|  | 108 | +            await writer.WriteAsync(idempotentResponse.ResponseBody); | 
|  | 109 | +            await writer.FlushAsync(); | 
|  | 110 | + | 
|  | 111 | +            return; | 
|  | 112 | +        } | 
|  | 113 | + | 
|  | 114 | +        await using IOperationsTransaction transaction = | 
|  | 115 | +            await idempotencyProvider.BeginRequestAsync(idempotencyKey, requestFingerprint, httpContext.RequestAborted); | 
|  | 116 | + | 
|  | 117 | +        string responseBody = await CaptureResponseBodyAsync(httpContext, _next); | 
|  | 118 | + | 
|  | 119 | +        idempotentResponse = new IdempotentResponse(requestFingerprint, (HttpStatusCode)httpContext.Response.StatusCode, | 
|  | 120 | +            httpContext.Response.Headers[HeaderNames.Location], httpContext.Response.ContentType, responseBody); | 
|  | 121 | + | 
|  | 122 | +        await idempotencyProvider.CompleteRequestAsync(idempotencyKey, idempotentResponse, transaction, httpContext.RequestAborted); | 
|  | 123 | +    } | 
|  | 124 | + | 
|  | 125 | +    private static string? GetIdempotencyKey(IHeaderDictionary requestHeaders) | 
|  | 126 | +    { | 
|  | 127 | +        if (!requestHeaders.ContainsKey(HeaderConstants.IdempotencyKey)) | 
|  | 128 | +        { | 
|  | 129 | +            return null; | 
|  | 130 | +        } | 
|  | 131 | + | 
|  | 132 | +        string headerValue = requestHeaders[HeaderConstants.IdempotencyKey]; | 
|  | 133 | + | 
|  | 134 | +        if (headerValue.Length >= 2 && headerValue[0] == '\"' && headerValue[^1] == '\"') | 
|  | 135 | +        { | 
|  | 136 | +            return headerValue[1..^1]; | 
|  | 137 | +        } | 
|  | 138 | + | 
|  | 139 | +        return string.Empty; | 
|  | 140 | +    } | 
|  | 141 | + | 
|  | 142 | +    [AssertionMethod] | 
|  | 143 | +    private static void AssertIdempotencyKeyIsValid([SysNotNull] string? idempotencyKey) | 
|  | 144 | +    { | 
|  | 145 | +        if (idempotencyKey == null) | 
|  | 146 | +        { | 
|  | 147 | +            throw new JsonApiException(new ErrorObject(HttpStatusCode.BadRequest) | 
|  | 148 | +            { | 
|  | 149 | +                Title = $"Missing '{HeaderConstants.IdempotencyKey}' HTTP header.", | 
|  | 150 | +                Detail = "An idempotency key is a unique value generated by the client, which the server uses to recognize subsequent retries " + | 
|  | 151 | +                    "of the same request. This should be a random string with enough entropy to avoid collisions." | 
|  | 152 | +            }); | 
|  | 153 | +        } | 
|  | 154 | + | 
|  | 155 | +        if (idempotencyKey == string.Empty) | 
|  | 156 | +        { | 
|  | 157 | +            throw new JsonApiException(new ErrorObject(HttpStatusCode.BadRequest) | 
|  | 158 | +            { | 
|  | 159 | +                Title = $"Invalid '{HeaderConstants.IdempotencyKey}' HTTP header.", | 
|  | 160 | +                Detail = "Expected non-empty value surrounded by double quotes.", | 
|  | 161 | +                Source = new ErrorSource | 
|  | 162 | +                { | 
|  | 163 | +                    Header = HeaderConstants.IdempotencyKey | 
|  | 164 | +                } | 
|  | 165 | +            }); | 
|  | 166 | +        } | 
|  | 167 | +    } | 
|  | 168 | + | 
|  | 169 | +    /// <summary> | 
|  | 170 | +    /// Enables to read the HTTP request stream multiple times, without risking GC Gen2/LOH promotion. | 
|  | 171 | +    /// </summary> | 
|  | 172 | +    private static async Task BufferRequestBodyAsync(HttpContext httpContext) | 
|  | 173 | +    { | 
|  | 174 | +        // Above this threshold, EnableBuffering() switches to a temporary file on disk. | 
|  | 175 | +        // Source: Microsoft.AspNetCore.Http.BufferingHelper.DefaultBufferThreshold | 
|  | 176 | +        const int enableBufferingThreshold = 1024 * 30; | 
|  | 177 | + | 
|  | 178 | +        if (httpContext.Request.ContentLength > enableBufferingThreshold) | 
|  | 179 | +        { | 
|  | 180 | +            httpContext.Request.EnableBuffering(enableBufferingThreshold); | 
|  | 181 | +        } | 
|  | 182 | +        else | 
|  | 183 | +        { | 
|  | 184 | +            MemoryStream memoryRequestBodyStream = MemoryStreamManager.GetStream(); | 
|  | 185 | +            await httpContext.Request.Body.CopyToAsync(memoryRequestBodyStream, httpContext.RequestAborted); | 
|  | 186 | +            memoryRequestBodyStream.Seek(0, SeekOrigin.Begin); | 
|  | 187 | + | 
|  | 188 | +            httpContext.Request.Body = memoryRequestBodyStream; | 
|  | 189 | +            httpContext.Response.RegisterForDispose(memoryRequestBodyStream); | 
|  | 190 | +        } | 
|  | 191 | +    } | 
|  | 192 | + | 
|  | 193 | +    private async Task<string> GetRequestFingerprintAsync(HttpContext httpContext) | 
|  | 194 | +    { | 
|  | 195 | +        using var reader = new StreamReader(httpContext.Request.Body, leaveOpen: true); | 
|  | 196 | +        string requestBody = await reader.ReadToEndAsync(); | 
|  | 197 | +        httpContext.Request.Body.Seek(0, SeekOrigin.Begin); | 
|  | 198 | + | 
|  | 199 | +        return _fingerprintGenerator.Generate(ArrayFactory.Create(httpContext.Request.GetEncodedUrl(), requestBody)); | 
|  | 200 | +    } | 
|  | 201 | + | 
|  | 202 | +    /// <summary> | 
|  | 203 | +    /// Executes the specified action and returns what it wrote to the HTTP response stream. | 
|  | 204 | +    /// </summary> | 
|  | 205 | +    private static async Task<string> CaptureResponseBodyAsync(HttpContext httpContext, RequestDelegate nextAction) | 
|  | 206 | +    { | 
|  | 207 | +        // Loosely based on https://elanderson.net/2019/12/log-requests-and-responses-in-asp-net-core-3/. | 
|  | 208 | + | 
|  | 209 | +        Stream previousResponseBodyStream = httpContext.Response.Body; | 
|  | 210 | + | 
|  | 211 | +        try | 
|  | 212 | +        { | 
|  | 213 | +            await using MemoryStream memoryResponseBodyStream = MemoryStreamManager.GetStream(); | 
|  | 214 | +            httpContext.Response.Body = memoryResponseBodyStream; | 
|  | 215 | + | 
|  | 216 | +            try | 
|  | 217 | +            { | 
|  | 218 | +                await nextAction(httpContext); | 
|  | 219 | +            } | 
|  | 220 | +            finally | 
|  | 221 | +            { | 
|  | 222 | +                memoryResponseBodyStream.Seek(0, SeekOrigin.Begin); | 
|  | 223 | +                await memoryResponseBodyStream.CopyToAsync(previousResponseBodyStream); | 
|  | 224 | +            } | 
|  | 225 | + | 
|  | 226 | +            memoryResponseBodyStream.Seek(0, SeekOrigin.Begin); | 
|  | 227 | +            using var streamReader = new StreamReader(memoryResponseBodyStream, leaveOpen: true); | 
|  | 228 | +            return await streamReader.ReadToEndAsync(); | 
|  | 229 | +        } | 
|  | 230 | +        finally | 
|  | 231 | +        { | 
|  | 232 | +            httpContext.Response.Body = previousResponseBodyStream; | 
|  | 233 | +        } | 
|  | 234 | +    } | 
|  | 235 | + | 
|  | 236 | +    private static async Task FlushResponseAsync(HttpResponse httpResponse, JsonSerializerOptions serializerOptions, ErrorObject error) | 
|  | 237 | +    { | 
|  | 238 | +        httpResponse.ContentType = HeaderConstants.MediaType; | 
|  | 239 | +        httpResponse.StatusCode = (int)error.StatusCode; | 
|  | 240 | + | 
|  | 241 | +        var errorDocument = new Document | 
|  | 242 | +        { | 
|  | 243 | +            Errors = error.AsList() | 
|  | 244 | +        }; | 
|  | 245 | + | 
|  | 246 | +        await JsonSerializer.SerializeAsync(httpResponse.Body, errorDocument, serializerOptions); | 
|  | 247 | +        await httpResponse.Body.FlushAsync(); | 
|  | 248 | +    } | 
|  | 249 | +} | 
0 commit comments