Git Product home page Git Product logo

Comments (4)

ioquatix avatar ioquatix commented on June 2, 2024

Without digging into it, it appears that the latter @length += chunk.bytesize is incorrect. Do you think removing it is the correct course of action?

from rack.

mattbrictson avatar mattbrictson commented on June 2, 2024

I'm actually not sure I understand the intention of the existing code, in particular why @buffered is set to nil in the constructor and lazily initialized later. Since I may be missing the point of @buffered, take what follows with a grain of salt.

The problem I see is that there is a missing assignment of @buffered in one of the conditional branches within buffered_body!.

Abbreviated, the code currently looks like this:

if @buffered.nil?
  if @body.is_a?(Array)
    # ...
    # ⚠️ @buffered is not set in this branch
  elsif @body.respond_to?(:each)
    # ...
    @buffered = true
  else
    @buffered = false
  end
end

It seems like the point of this code was to lazily init @buffered to true or false, but currently that isn't happening in the first branch. So I think the fix is to add @buffered = true, like this:

--- lib/rack/response.rb
+++ lib/rack/response.rb
@@ -318,12 +318,14 @@ module Rack
           if @body.is_a?(Array)
             # The user supplied body was an array:
             @body = @body.compact
             @body.each do |part|
               @length += part.to_s.bytesize
             end
+
+            @buffered = true
           elsif @body.respond_to?(:each)
             # Turn the user supplied body into a buffered array:
             body = @body
             @body = Array.new
 
             body.each do |part|

With this one-line change, I get the correct content-length result when running my reproduction script.

from rack.

ioquatix avatar ioquatix commented on June 2, 2024

Do you mind making a PR?

from rack.

mattbrictson avatar mattbrictson commented on June 2, 2024

OK, I've opened PR #2150

from rack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.