Method: Rack::Lint#check_content_length

Defined in:
lib/rack/lint.rb

#check_content_length(status, headers, env) ⇒ Object

The Content-Length



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/rack/lint.rb', line 455

def check_content_length(status, headers, env)
  headers.each { |key, value|
    if key.downcase == 'content-length'
      ## There must not be a <tt>Content-Length</tt> header when the
      ## +Status+ is 1xx, 204 or 304.
      assert("Content-Length header found in #{status} response, not allowed") {
        not Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include? status.to_i
      }

      bytes = 0
      string_body = true

      if @body.respond_to?(:to_ary)
        @body.each { |part|
          unless part.kind_of?(String)
            string_body = false
            break
          end

          bytes += Rack::Utils.bytesize(part)
        }

        if env["REQUEST_METHOD"] == "HEAD"
          assert("Response body was given for HEAD request, but should be empty") {
            bytes == 0
          }
        else
          if string_body
            assert("Content-Length header was #{value}, but should be #{bytes}") {
              value == bytes.to_s
            }
          end
        end
      end

      return
    end
  }
end