ikyriak / IdempotentAPI

A .NET library that handles the HTTP write operations (POST and PATCH) that can affect only once for the given request data and idempotency-key by using an ASP.NET Core attribute (filter).
MIT License
270 stars 39 forks source link

Failed to get correct hash value with same request body #38

Closed bernardiego closed 2 years ago

bernardiego commented 2 years ago

I came across a bug in which for a request body big enough (30kb+), the cache wasn't able to get fetched properly because of a different hash string.

In Idempotency.cs -> GetRequestsDataHash(HttpRequest httpRequest) we just only have to add

httpRequest.EnableBuffering();

and then, make the copy asynchronously to the memoryStream.

The complete code:

if (httpRequest.ContentLength.HasValue
    && httpRequest.Body != null)
{
    // 2022-08-18: Enable buffering for bodies greater than 30k in size.
    //             Then, read the buffer asynchronously.
    httpRequest.EnableBuffering();

    if (httpRequest.Body.CanRead
        && httpRequest.Body.CanSeek)
    {
        using MemoryStream memoryStream = new();
        httpRequest.Body.Position = 0;
        var copy = httpRequest.Body.CopyToAsync(memoryStream);
        copy.Wait();
        requestsData.Add(memoryStream.ToArray());
    }
}
ikyriak commented 2 years ago

Hello @bernardiego Thanks for taking the time to report and provide a fix for this issue! 💪 🙏 I will reproduce it and include this fix in the following release.

ikyriak commented 2 years ago

Hello @bernardiego

Thank you again for your contribution 🙏. Your fix has been included in the v2.0.0-RC.01 🎆🎉.