diff options
author | Heiko Bernloehr <Heiko.Bernloehr@FreeIT.de> | 2018-01-18 07:30:51 +0100 |
---|---|---|
committer | Heiko Bernloehr <Heiko.Bernloehr@FreeIT.de> | 2018-01-18 07:44:34 +0100 |
commit | 60d878d0a0bfd7ba89aae8fc7cf4dac6400fb58e (patch) | |
tree | 0b3b890fd84d88c3f0e509c66837526d76ad7992 /app | |
parent | c1ee8a1f275918ea173d176ac4fa9378bfa2b0c1 (diff) | |
download | ecs-60d878d0a0bfd7ba89aae8fc7cf4dac6400fb58e.tar.gz ecs-60d878d0a0bfd7ba89aae8fc7cf4dac6400fb58e.zip |
Content-Length header via Rails middleware.
The Content-Length rack module provided through ruby 2.1 doesn't work
correctly with Rails 4.1. So we provide our own.
Diffstat (limited to 'app')
-rw-r--r-- | app/middleware/content_length.rb | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/app/middleware/content_length.rb b/app/middleware/content_length.rb new file mode 100644 index 0000000..fa51160 --- /dev/null +++ b/app/middleware/content_length.rb @@ -0,0 +1,32 @@ +require 'rack/utils' + +class ContentLength + + include Rack::Utils + + def initialize app + @app = app + 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'] + + #see https://stackoverflow.com/questions/26534165/unable-to-get-content-length-header-working-under-rails-4-1puma + #see https://github.com/rails/rails/pull/16793 + #&& body.respond_to?(:to_ary) + + obody = body + body, length = [], 0 + obody.each { |part| body << part; length += bytesize(part) } + obody.close if obody.respond_to?(:close) + + headers['Content-Length'] = length.to_s + end + [status, headers, body] + end + +end |