Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created October 2, 2015 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenderlove/7f8d7ba1f4bfb5a4b26c to your computer and use it in GitHub Desktop.
Save tenderlove/7f8d7ba1f4bfb5a4b26c to your computer and use it in GitHub Desktop.
diff --git a/Gemfile b/Gemfile
index 97406be..96dc4fc 100644
--- a/Gemfile
+++ b/Gemfile
@@ -11,7 +11,7 @@ gem 'concurrent-ruby', '~> 1.0.0.pre3', github: 'ruby-concurrency/concurrent-rub
# Active Job depends on the URI::GID::MissingModelIDError, which isn't released yet.
gem 'globalid', github: 'rails/globalid', branch: 'master'
-gem 'rack', github: 'rack/rack', branch: 'master'
+gem 'rack', path: '/Users/aaron/git/rack'
# This needs to be with require false as it is
# loaded after loading the test library to
diff --git a/Gemfile.lock b/Gemfile.lock
index 7d6db04..486937f 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -28,14 +28,6 @@ GIT
mime-types (>= 1.16, < 3)
GIT
- remote: git://github.com/rack/rack.git
- revision: c28f271d0c91f45e13bfa8f07bed445ef91f41de
- branch: master
- specs:
- rack (2.0.0.alpha)
- json
-
-GIT
remote: git://github.com/rails/arel.git
revision: 77ec13b46af2926bfcfc3073685711c874b0d272
branch: master
@@ -167,6 +159,12 @@ PATH
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
+PATH
+ remote: /Users/aaron/git/rack
+ specs:
+ rack (2.0.0.alpha)
+ json
+
GEM
remote: https://rubygems.org/
specs:
diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb
index f6f63f1..19a6f8c 100644
--- a/actionpack/lib/action_dispatch/http/response.rb
+++ b/actionpack/lib/action_dispatch/http/response.rb
@@ -39,7 +39,7 @@ module ActionDispatch # :nodoc:
end
def []=(k,v)
- if @response.committed?
+ if @response.sending? || @response.sent?
raise ActionDispatch::IllegalStateError, 'header already sent'
end
@@ -142,6 +142,9 @@ module ActionDispatch # :nodoc:
super()
@header = Header.new(self, header)
+ @header[Rack::RACK_HIJACK] = lambda { |io|
+ each { |chunk| io.write chunk }
+ }
self.body, self.status = body, status
@@ -156,6 +159,10 @@ module ActionDispatch # :nodoc:
yield self if block_given?
end
+ def write(string)
+ @buffer << string
+ end
+
def have_header?(key); headers.key? key; end
def get_header(key); headers[key]; end
def set_header(key, v); headers[key] = v; end
diff --git a/actionpack/lib/action_dispatch/journey/router.rb b/actionpack/lib/action_dispatch/journey/router.rb
index c570de2..d9f4879 100644
--- a/actionpack/lib/action_dispatch/journey/router.rb
+++ b/actionpack/lib/action_dispatch/journey/router.rb
@@ -39,7 +39,7 @@ module ActionDispatch
req.path_parameters = set_params.merge parameters
- status, headers, body = route.app.serve(req)
+ status, headers, body = route.app.serve(req, res)
if 'pass' == headers['X-Cascade']
req.script_name = script_name
diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb
index 2889aca..3149d46 100644
--- a/actionpack/lib/action_dispatch/middleware/cookies.rb
+++ b/actionpack/lib/action_dispatch/middleware/cookies.rb
@@ -589,6 +589,20 @@ module ActionDispatch
@app = app
end
+ def serve(request, res)
+ @app.serve request, res
+
+ if request.have_cookie_jar?
+ cookie_jar = request.cookie_jar
+ unless cookie_jar.committed?
+ cookie_jar.write(res.headers)
+ if res.headers[HTTP_HEADER].respond_to?(:join)
+ res.headers[HTTP_HEADER] = res.headers[HTTP_HEADER].join("\n")
+ end
+ end
+ end
+ end
+
def call(env)
request = ActionDispatch::Request.new env
diff --git a/actionpack/lib/action_dispatch/middleware/request_id.rb b/actionpack/lib/action_dispatch/middleware/request_id.rb
index 1555ff7..de760fb 100644
--- a/actionpack/lib/action_dispatch/middleware/request_id.rb
+++ b/actionpack/lib/action_dispatch/middleware/request_id.rb
@@ -18,6 +18,12 @@ module ActionDispatch
@app = app
end
+ def serve(req, res)
+ req.request_id = make_request_id(req.x_request_id)
+ res.set_header X_REQUEST_ID, req.request_id
+ @app.serve req, res
+ end
+
def call(env)
req = ActionDispatch::Request.new env
req.request_id = make_request_id(req.x_request_id)
diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb
index eea8bb1..4e05604 100644
--- a/actionpack/lib/action_dispatch/middleware/stack.rb
+++ b/actionpack/lib/action_dispatch/middleware/stack.rb
@@ -47,6 +47,7 @@ module ActionDispatch
req = env['_REQUEST']
res = env['_RESPONSE']
app.serve req, res
+ [res.status, res.headers, Object.new]
end
end
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb
index 74d9a53..23b7efb 100644
--- a/actionpack/lib/action_dispatch/routing/route_set.rb
+++ b/actionpack/lib/action_dispatch/routing/route_set.rb
@@ -26,10 +26,9 @@ module ActionDispatch
def dispatcher?; true; end
- def serve(req)
+ def serve(req, res)
params = req.path_parameters
controller = controller req
- res = controller.make_response! req
dispatch(controller, params[:action], req, res)
rescue NameError => e
if @raise_on_name_error
diff --git a/actionview/lib/action_view/digestor.rb b/actionview/lib/action_view/digestor.rb
index 12e9723..a643102 100644
--- a/actionview/lib/action_view/digestor.rb
+++ b/actionview/lib/action_view/digestor.rb
@@ -13,6 +13,11 @@ module ActionView
ActionView::Digestor.cache.clear
app.call(env)
end
+
+ def serve(req, res)
+ ActionView::Digestor.cache.clear
+ app.serve(req, res)
+ end
end
class << self
diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb
index dcb2bd3..2099e03 100644
--- a/activerecord/lib/active_record/query_cache.rb
+++ b/activerecord/lib/active_record/query_cache.rb
@@ -27,6 +27,19 @@ module ActiveRecord
@app = app
end
+ def serve(req, res)
+ connection = ActiveRecord::Base.connection
+ enabled = connection.query_cache_enabled
+ connection_id = ActiveRecord::Base.connection_id
+ connection.enable_query_cache!
+
+ res.after_finish do
+ restore_query_cache_settings(connection_id, enabled)
+ end
+
+ @app.serve req, res
+ end
+
def call(env)
connection = ActiveRecord::Base.connection
enabled = connection.query_cache_enabled
diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb
index 4580f31..c8ed257 100644
--- a/railties/lib/rails/engine.rb
+++ b/railties/lib/rails/engine.rb
@@ -717,6 +717,7 @@ module Rails
end
def build_response(req)
+ ActionDispatch::Response.create
end
def build_middleware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment