Skip to content

fix render: write content length in Data.Render #4206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dengaleev
Copy link

This PR fixes an issue where the Content-Length header is not set correctly for large data written by ResponseWriter.Write. According to the Go documentation, the Content-Length header is automatically added only for small data sizes. For larger data, it is necessary to explicitly set the Content-Length header to ensure proper behavior.

@dengaleev
Copy link
Author

Hi, @appleboy

Could you check this pull request?

Copy link

codecov bot commented Apr 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.00%. Comparing base (3dc1cd6) to head (c4e549a).
Report is 104 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4206      +/-   ##
==========================================
- Coverage   99.21%   99.00%   -0.22%     
==========================================
  Files          42       44       +2     
  Lines        3182     3405     +223     
==========================================
+ Hits         3157     3371     +214     
- Misses         17       24       +7     
- Partials        8       10       +2     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags "sonic avx" 98.99% <100.00%> (?)
-tags go_json 98.99% <100.00%> (?)
-tags nomsgpack 98.98% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.23 99.00% <100.00%> (?)
go-1.24 99.00% <100.00%> (?)
macos-latest 99.00% <100.00%> (-0.22%) ⬇️
ubuntu-latest 99.00% <100.00%> (-0.22%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@appleboy appleboy added the bug label Apr 20, 2025
@appleboy appleboy added this to the v1.11 milestone Apr 20, 2025
@appleboy
Copy link
Member

I will take it.

@appleboy appleboy requested a review from Copilot May 20, 2025 15:03
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an issue with the Content-Length header in Data.Render to ensure that it is set correctly for both large and empty data responses.

  • Updated Data.Render to explicitly set the Content-Length header using strconv.Itoa(len(r.Data)).
  • Added tests in render_test.go to verify the header for various data sizes, including zero-length.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
render/render_test.go Added tests to validate the Content-Length header for different data sizes.
render/data.go Modified Render to set the Content-Length header based on the data length.

Comment on lines +21 to +23
if len(r.Data) > 0 {
w.Header().Set("Content-Length", strconv.Itoa(len(r.Data)))
}
Copy link
Preview

Copilot AI May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition 'if len(r.Data) > 0' prevents setting the Content-Length header when the data length is 0, causing test failures for zero-length responses. Consider removing the condition so the header is always set to strconv.Itoa(len(r.Data)).

Suggested change
if len(r.Data) > 0 {
w.Header().Set("Content-Length", strconv.Itoa(len(r.Data)))
}
w.Header().Set("Content-Length", strconv.Itoa(len(r.Data)))

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants