From fd9fc1455ec7a5885d32c15c43643bea593efcda Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Sat, 8 May 2010 23:42:40 -0500 Subject: [PATCH] Prefer Monkey-patching Rack Gem to Vendored Rack This gets around a dreaded in `load_missing_constant': Rack is not missing constant Handler! (ArgumentError) error in latest Ruby 1.9.2-dev. (Ruby 1.8.x doesn't seem to care.) --- config/environment.rb | 3 + lib/rack_stuff.rb | 61 +++++++++++++++++++ .../rails/actionpack/lib/action_controller.rb | 2 +- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 lib/rack_stuff.rb diff --git a/config/environment.rb b/config/environment.rb index e233b1ca..f29cad63 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -58,8 +58,11 @@ end require_dependency 'instiki_errors' #require 'jcode' + +# Miscellaneous monkey patches (here be dragons ...) require 'caching_stuff' require 'logging_stuff' +require 'rack_stuff' #Additional Mime-types mime_types = YAML.load_file(File.join(File.dirname(__FILE__), 'mime_types.yml')) diff --git a/lib/rack_stuff.rb b/lib/rack_stuff.rb new file mode 100644 index 00000000..2c9ab92d --- /dev/null +++ b/lib/rack_stuff.rb @@ -0,0 +1,61 @@ +require 'webrick' +require 'stringio' +require 'rack/content_length' +require 'tempfile' + +module Rack + module Handler + class WEBrick < ::WEBrick::HTTPServlet::AbstractServlet + def self.run(app, options={}) + options[:BindAddress] = options.delete(:Host) if options[:Host] + server = ::WEBrick::HTTPServer.new(options) + server.mount "/", Rack::Handler::WEBrick, app + trap(:INT) { server.shutdown } + trap(:TERM) { server.shutdown } + yield server if block_given? + server.start + end + end + end + + class RewindableInput + def make_rewindable + # Buffer all data into a tempfile. Since this tempfile is private to this + # RewindableInput object, we chmod it so that nobody else can read or write + # it. On POSIX filesystems we also unlink the file so that it doesn't + # even have a file entry on the filesystem anymore, though we can still + # access it because we have the file handle open. + @rewindable_io = Tempfile.new('RackRewindableInput') + @rewindable_io.chmod(0000) + @rewindable_io.set_encoding(Encoding::BINARY) if @rewindable_io.respond_to?(:set_encoding) + @rewindable_io.binmode + if filesystem_has_posix_semantics? && !tempfile_unlink_contains_bug? + @rewindable_io.unlink + @unlinked = true + end + + buffer = "" + while @io.read(1024 * 4, buffer) + entire_buffer_written_out = false + while !entire_buffer_written_out + written = @rewindable_io.write(buffer) + entire_buffer_written_out = written == buffer.size + if !entire_buffer_written_out + buffer.slice!(0 .. written - 1) + end + end + end + @rewindable_io.rewind + end + + def tempfile_unlink_contains_bug? + # The tempfile library as included in Ruby 1.9.1-p152 and later + # contains a bug: unlinking an open Tempfile object also closes + # it, which breaks our expected POSIX semantics. This problem + # has been fixed in Ruby 1.9.2, but the Ruby team chose not to + # include the bug fix in later versions of the 1.9.1 series. + ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby" + ruby_engine == "ruby" && RUBY_VERSION == "1.9.1" && RUBY_PATCHLEVEL >= 152 + end + end +end \ No newline at end of file diff --git a/vendor/rails/actionpack/lib/action_controller.rb b/vendor/rails/actionpack/lib/action_controller.rb index c18303b0..dbf13ae0 100644 --- a/vendor/rails/actionpack/lib/action_controller.rb +++ b/vendor/rails/actionpack/lib/action_controller.rb @@ -32,7 +32,7 @@ rescue LoadError end begin - gem 'rack', '~> 1.1.1' + gem 'rack', '~> 1.1.0' require 'rack' rescue Gem::LoadError $:.unshift "#{File.dirname(__FILE__)}/../../../plugins/rack/lib"