Skip to content

Commit 48624ff

Browse files
committed
Support only Rack 2.x with Rails, removing old 1.x rack/rails compat
Rack 2.x wraps the @env within @Req so previous overrides were not working correctly in all cases.
1 parent b813106 commit 48624ff

File tree

4 files changed

+25
-83
lines changed

4 files changed

+25
-83
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ group :default do
44
if rack_version = ENV['RACK_VERSION']
55
gem 'rack', rack_version
66
else
7-
gem 'rack', '< 3.0'
7+
gem 'rack', '~> 2.2', '< 3.0'
88
end
99
end
1010

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ PLATFORMS
2929

3030
DEPENDENCIES
3131
appraisal (< 1.0)
32-
rack (< 3.0)
32+
rack (~> 2.2, < 3.0)
3333
rake (~> 13.2)
3434
rspec
3535

src/main/ruby/jruby/rack/session_store.rb

Lines changed: 21 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,74 +10,38 @@
1010
module JRuby::Rack
1111
module Session
1212

13-
if defined?(::Rack::Session::Abstract::SessionHash) # Rack 1.3+
13+
class SessionHash < ::Rack::Session::Abstract::SessionHash
1414

15-
# as of rails 3.1.x the rack session hash implementation is used
16-
# rather than the custom rails AbstractStore::SessionHash class
17-
class SessionHash < ::Rack::Session::Abstract::SessionHash; end
18-
19-
# 1.5.0 removed SessionHash http://github.com/rack/rack/commit/83a270d64820
20-
OptionsHash = if defined?(::Rack::Session::Abstract::OptionsHash)
21-
::Rack::Session::Abstract::OptionsHash
22-
else nil
15+
def enabled? # Rails 7.0 added a need for this in Sessions, and forgot to make it optional within flash middleware
16+
true
2317
end
2418

25-
elsif defined?(ActionDispatch::Session::AbstractStore) # Rails 3.0
26-
27-
require 'active_support/core_ext/hash' # non-loaded SessionHash dependency
28-
29-
class SessionHash < ActionDispatch::Session::AbstractStore::SessionHash; end
30-
31-
OptionsHash = ActionDispatch::Session::AbstractStore::OptionsHash
32-
33-
else # a fallback for (old) Rails 2.3
34-
35-
class SessionHash < ActionController::Session::AbstractStore::SessionHash; end
36-
37-
OptionsHash = ActionController::Session::AbstractStore::OptionsHash
38-
39-
end
40-
41-
SessionHash.class_eval do
42-
4319
# Allows direct delegation to servlet session methods when session is active
4420
def method_missing(method, *args, &block)
45-
servlet_session = store.get_servlet_session(@env)
21+
servlet_session = @store.get_servlet_session(@req.env)
4622
if servlet_session && servlet_session.respond_to?(method)
4723
servlet_session.send(method, *args, &block)
4824
else
4925
super
5026
end
5127
end
52-
53-
private
54-
def store
55-
@store ||= defined?(@store) ? @store : @by # Rack 1.5 renamed @by
56-
end
57-
5828
end
5929

6030
# Rack based SessionStore implementation but compatible with (older) AbstractStore.
6131
class SessionStore < ::Rack::Session::Abstract::ID
6232

63-
ENV_SESSION_KEY = defined?(::Rack::Session::Abstract::ENV_SESSION_KEY) ?
64-
::Rack::Session::Abstract::ENV_SESSION_KEY : 'rack.session'.freeze
65-
66-
ENV_SESSION_OPTIONS_KEY = defined?(::Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY) ?
67-
::Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY : 'rack.session.options'.freeze
68-
6933
ENV_SERVLET_SESSION_KEY = 'java.servlet_session'.freeze
70-
7134
RAILS_SESSION_KEY = "__current_rails_session".freeze
7235

7336
def initialize(app, options={})
7437
super(app, options.merge!(:cookie_only => false, :defer => true))
7538
end
7639

77-
def context(env, app = @app) # adapt Rack 1.1/1.2 to be compatible with 1.3+
78-
prepare_session(env)
79-
status, headers, body = app.call(env)
80-
commit_session(env, status, headers, body)
40+
def context(env, app = @app)
41+
req = make_request env
42+
prepare_session(req)
43+
status, headers, body = app.call(req.env)
44+
commit_session(req, status, headers, body)
8145
end
8246

8347
# (public) servlet specific methods :
@@ -117,20 +81,9 @@ def generate_sid(secure = @sid_secure)
11781
nil # dummy method - no session id generation with servlet API
11882
end
11983

120-
def prepare_session(env) # exist since Rack 1.3
121-
session_was = env[ENV_SESSION_KEY]
122-
env[ENV_SESSION_KEY] = session_class.new(self, env)
123-
if options_hash = ::JRuby::Rack::Session::OptionsHash
124-
env[ENV_SESSION_OPTIONS_KEY] = options_hash.new(self, env, @default_options)
125-
else
126-
env[ENV_SESSION_OPTIONS_KEY] = @default_options.dup
127-
end
128-
env[ENV_SESSION_KEY].merge! session_was if session_was
129-
end
130-
131-
def load_session(env, session_id = nil) # session_id arg for get_session alias
84+
def load_session(req) # session_id arg for get_session alias
13285
session_id, session = false, {}
133-
if servlet_session = get_servlet_session(env)
86+
if servlet_session = get_servlet_session(req.env)
13487
begin
13588
session_id = servlet_session.getId
13689
servlet_session.synchronized do
@@ -152,42 +105,36 @@ def load_session(env, session_id = nil) # session_id arg for get_session alias
152105
end
153106
[ session_id, session ]
154107
end
155-
alias :get_session :load_session # for AbstractStore::SessionHash compatibility
156108

157-
def extract_session_id(env)
158-
servlet_session = get_servlet_session(env)
109+
def extract_session_id(req)
110+
servlet_session = get_servlet_session(req.env)
159111
servlet_session.getId rescue nil if servlet_session
160112
end
161113

162-
def current_session_id(env)
163-
env[ENV_SESSION_OPTIONS_KEY][:id] # 1.5.0: env[ENV_SESSION_KEY].id
164-
end if ::JRuby::Rack::Session::OptionsHash
165-
166-
def session_exists?(env)
167-
value = current_session_id(env)
114+
def session_exists?(req)
115+
value = current_session_id(req)
168116
value && ! value.empty?
169117
end
170-
alias :exists? :session_exists? # for AbstractStore::SessionHash compatibility
171118

172119
def loaded_session?(session)
173120
! session.is_a?(::JRuby::Rack::Session::SessionHash) || session.loaded?
174121
end
175122

176-
def commit_session(env, status, headers, body)
177-
session = env[ENV_SESSION_KEY]
178-
options = env[ENV_SESSION_OPTIONS_KEY]
123+
def commit_session(req, status, headers, body)
124+
session = req.env[::Rack::RACK_SESSION]
125+
options = req.env[::Rack::RACK_SESSION_OPTIONS]
179126

180127
if options[:drop] || options[:renew]
181-
destroy_session(env, options[:id], options)
128+
destroy_session(req.env, options[:id], options)
182129
end
183130

184131
return [status, headers, body] if options[:drop] || options[:skip]
185132

186133
if loaded_session?(session)
187134
session_id = session.respond_to?(:id=) ? session.id : options[:id]
188135
session_data = session.to_hash.delete_if { |_,v| v.nil? }
189-
unless set_session(env, session_id, session_data, options)
190-
env["rack.errors"].puts("WARNING #{self.class.name} failed to save session. Content dropped.")
136+
unless set_session(req.env, session_id, session_data, options)
137+
req.env["rack.errors"].puts("WARNING #{self.class.name} failed to save session. Content dropped.")
191138
end
192139
end
193140

@@ -245,7 +192,6 @@ def destroy_session(env, session_id = nil, options = nil)
245192
rescue java.lang.IllegalStateException # if session already invalid
246193
nil
247194
end
248-
alias :destroy :destroy_session # for AbstractStore::SessionHash compatibility
249195

250196
end
251197

src/spec/ruby/action_controller/session/java_servlet_store_spec.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
@session_store = ActionController::Session::JavaServletStore.new(@app)
3333
end
3434

35-
it "should raise an error if the servlet request is not present" do
36-
expect { @session_store.call({}) }.to raise_error(RuntimeError)
37-
end
38-
3935
it "should do nothing if the session is not accessed" do
4036
@app.should_receive(:call)
4137
@session_store.call(@env)
@@ -95,7 +91,7 @@
9591
@request.should_receive(:getSession).with(false).and_return @session
9692
@app.should_receive(:call)
9793
@session_store.call(@env)
98-
@session_store.send(:extract_session_id, @env).should == @session_id
94+
expect(@session_store.send(:extract_session_id, Rack::Request.new(@env))).to eq @session_id
9995
end
10096

10197
it "should retrieve the marshalled session from the java session" do
@@ -313,7 +309,7 @@ def new_session_hash(*args)
313309
else
314310
store = @session_store; env = args[0];
315311
end
316-
::JRuby::Rack::Session::SessionHash.new(store, env)
312+
::JRuby::Rack::Session::SessionHash.new(store, ::Rack::Request.new(env))
317313
end
318314

319315
end if defined? Rails

0 commit comments

Comments
 (0)