aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/rack/content_length.rb
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib/rails/rack/content_length.rb')
-rw-r--r--railties/lib/rails/rack/content_length.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/railties/lib/rails/rack/content_length.rb b/railties/lib/rails/rack/content_length.rb
new file mode 100644
index 0000000000..6839af4152
--- /dev/null
+++ b/railties/lib/rails/rack/content_length.rb
@@ -0,0 +1,38 @@
+require 'action_dispatch'
+require 'rack/utils'
+
+module Rails
+ module Rack
+ # Sets the Content-Length header on responses with fixed-length bodies.
+ class ContentLength
+ include ::Rack::Utils
+
+ def initialize(app, sendfile=nil)
+ @app = app
+ @sendfile = sendfile
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+ headers = HeaderHash.new(headers)
+
+ if !STATUS_WITH_NO_ENTITY_BODY.include?(status.to_i) &&
+ !headers['Content-Length'] &&
+ !headers['Transfer-Encoding'] &&
+ !(@sendfile && headers[@sendfile])
+
+ old_body = body
+ body, length = [], 0
+ old_body.each do |part|
+ body << part
+ length += bytesize(part)
+ end
+ old_body.close if old_body.respond_to?(:close)
+ headers['Content-Length'] = length.to_s
+ end
+
+ [status, headers, body]
+ end
+ end
+ end
+end \ No newline at end of file