Skip to content

Fix Content-Length for rack >= 2.1.0 #290

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

Merged
merged 1 commit into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/web_console/injector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ def initialize(body, headers)
end

def inject(content)
# Remove any previously set Content-Length header because we modify
# the body. Otherwise the response will be truncated.
@headers.delete("Content-Length")
# Set Content-Length header to the size of the current body
# + the extra content. Otherwise the response will be truncated.
if @headers["Content-Length"]
@headers["Content-Length"] = @body.bytesize + content.bytesize
end

[
if position = @body.rindex("</body>")
Expand Down
4 changes: 2 additions & 2 deletions test/web_console/injector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class InjectorTest < ActiveSupport::TestCase
assert_equal [ [ "foobar" ], {} ], Injector.new(body, {}).inject("bar")
end

test "deletes the Content-Length header" do
test "updates the Content-Length header" do
body = [ "foo" ]
headers = { "Content-Length" => 3 }

assert_equal [ [ "foobar" ], {} ], Injector.new(body, headers).inject("bar")
assert_equal [ [ "foobar" ], { "Content-Length" => 6 } ], Injector.new(body, headers).inject("bar")
end
end
end
1 change: 1 addition & 0 deletions test/web_console/middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def headers
@app = Middleware.new(Application.new(response_content_length: 7))

get "/", params: nil

assert_equal(response.body.size, response.headers["Content-Length"].to_i)
end

Expand Down