diff options
author | James Tucker <jftucker@gmail.com> | 2014-07-05 15:40:01 -0700 |
---|---|---|
committer | James Tucker <jftucker@gmail.com> | 2014-07-05 15:40:01 -0700 |
commit | f2cbe32a1dd1863dd68fea79905acf4dad8a2c5f (patch) | |
tree | 56e6d8a1b222c66745853cef302c5d03689980a2 | |
parent | 50819562e647b0e3c76fd58ebb9fb0f92d14beef (diff) | |
download | rack-f2cbe32a1dd1863dd68fea79905acf4dad8a2c5f.tar.gz |
Monkey patch to fix WEBrick chunking semantics.
* Previously proposed in #707, unfortunately that patch caused double encoding * Fixes #707, #618 * The longevity of this patch is dubious. If WEBrick makes identical semantics modifications as I think should be done, this patch will have no effect. If WEBrick introduces changes to internal header handling, class structure, etc, we'll break.
-rw-r--r-- | lib/rack/handler/webrick.rb | 18 | ||||
-rw-r--r-- | test/spec_webrick.rb | 18 |
2 files changed, 36 insertions, 0 deletions
diff --git a/lib/rack/handler/webrick.rb b/lib/rack/handler/webrick.rb index f76679b4..023d8b27 100644 --- a/lib/rack/handler/webrick.rb +++ b/lib/rack/handler/webrick.rb @@ -2,6 +2,23 @@ require 'webrick' require 'stringio' require 'rack/content_length' +# This monkey patch allows for applications to perform their own chunking +# through WEBrick::HTTPResponse iff rack is set to true. +class WEBrick::HTTPResponse + attr_accessor :rack + + alias _rack_setup_header setup_header + def setup_header + app_chunking = rack && @header['transfer-encoding'] == 'chunked' + + @chunked = app_chunking if app_chunking + + _rack_setup_header + + @chunked = false if app_chunking + end +end + module Rack module Handler class WEBrick < ::WEBrick::HTTPServlet::AbstractServlet @@ -39,6 +56,7 @@ module Rack end def service(req, res) + res.rack = true env = req.meta_vars env.delete_if { |k, v| v.nil? } diff --git a/test/spec_webrick.rb b/test/spec_webrick.rb index b29a82d5..497bfe20 100644 --- a/test/spec_webrick.rb +++ b/test/spec_webrick.rb @@ -162,5 +162,23 @@ describe Rack::Handler::WEBrick do } end + should "produce correct HTTP semantics with and without app chunking" do + @server.mount "/chunked", Rack::Handler::WEBrick, + Rack::Lint.new(lambda{ |req| + [ + 200, + {"Transfer-Encoding" => "chunked"}, + ["7\r\nchunked\r\n0\r\n\r\n"] + ] + }) + + Net::HTTP.start(@host, @port){ |http| + res = http.get("/chunked") + res["Transfer-Encoding"].should.equal "chunked" + res["Content-Length"].should.equal nil + res.body.should.equal "chunked" + } + end + @server.shutdown end |