gin icon indicating copy to clipboard operation
gin copied to clipboard

feature: Add content-length header where it is possible without reading buffers

Open comebackoneyear opened this issue 1 year ago • 0 comments

The Content-Length header is missing for some JSON responses. After some investigation, I found the following comment in the net/http documentation that states that

...if the total size of all written
data is under a few KB, and there are no Flush calls, the
Content-Length header is added automatically.

This led to the conclusion that there is no Content-Length header for longer responses because they are longer than the 2048-byte limit. However, in a lot of cases, the renderer knows the length of the response (it's a []byte or string) This PR adds the Content-Length header in those cases.

This investigation was because we noticed gzip compression in our CDN wasn't working (Cloudfront), and we realized this was due to the missing Content-Length header. https://aws.amazon.com/premiumsupport/knowledge-center/cloudfront-troubleshoot-compressed-files/#:~:text=Check%20your%20CloudFront%20configuration,a%20value%20greater%20than%20zero.

I have added test cases for the renderers with the added Content-Length header. There are some inconsistencies in the actual renderers and how the tests are structured, so I adapted my code to the style of the current scope.

  • With pull requests:
    • Open your pull request against master
    • Your pull request should have no more than two commits, if not you should squash them.
    • It should pass all tests in the available continuous integration systems such as GitHub Actions.
    • You should add/modify tests to cover your proposed code changes.
    • If your pull request contains a new feature, please document it on the README.

comebackoneyear avatar Mar 08 '23 20:03 comebackoneyear