10
10
module JRuby ::Rack
11
11
module Session
12
12
13
- if defined? ( ::Rack ::Session ::Abstract ::SessionHash ) # Rack 1.3+
13
+ class SessionHash < ::Rack ::Session ::Abstract ::SessionHash
14
14
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
23
17
end
24
18
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
-
43
19
# Allows direct delegation to servlet session methods when session is active
44
20
def method_missing ( method , *args , &block )
45
- servlet_session = store . get_servlet_session ( @env )
21
+ servlet_session = @ store. get_servlet_session ( @req . env )
46
22
if servlet_session && servlet_session . respond_to? ( method )
47
23
servlet_session . send ( method , *args , &block )
48
24
else
49
25
super
50
26
end
51
27
end
52
-
53
- private
54
- def store
55
- @store ||= defined? ( @store ) ? @store : @by # Rack 1.5 renamed @by
56
- end
57
-
58
28
end
59
29
60
30
# Rack based SessionStore implementation but compatible with (older) AbstractStore.
61
31
class SessionStore < ::Rack ::Session ::Abstract ::ID
62
32
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
-
69
33
ENV_SERVLET_SESSION_KEY = 'java.servlet_session' . freeze
70
-
71
34
RAILS_SESSION_KEY = "__current_rails_session" . freeze
72
35
73
36
def initialize ( app , options = { } )
74
37
super ( app , options . merge! ( :cookie_only => false , :defer => true ) )
75
38
end
76
39
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 )
81
45
end
82
46
83
47
# (public) servlet specific methods :
@@ -117,20 +81,9 @@ def generate_sid(secure = @sid_secure)
117
81
nil # dummy method - no session id generation with servlet API
118
82
end
119
83
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
132
85
session_id , session = false , { }
133
- if servlet_session = get_servlet_session ( env )
86
+ if servlet_session = get_servlet_session ( req . env )
134
87
begin
135
88
session_id = servlet_session . getId
136
89
servlet_session . synchronized do
@@ -152,42 +105,36 @@ def load_session(env, session_id = nil) # session_id arg for get_session alias
152
105
end
153
106
[ session_id , session ]
154
107
end
155
- alias :get_session :load_session # for AbstractStore::SessionHash compatibility
156
108
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 )
159
111
servlet_session . getId rescue nil if servlet_session
160
112
end
161
113
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 )
168
116
value && ! value . empty?
169
117
end
170
- alias :exists? :session_exists? # for AbstractStore::SessionHash compatibility
171
118
172
119
def loaded_session? ( session )
173
120
! session . is_a? ( ::JRuby ::Rack ::Session ::SessionHash ) || session . loaded?
174
121
end
175
122
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 ]
179
126
180
127
if options [ :drop ] || options [ :renew ]
181
- destroy_session ( env , options [ :id ] , options )
128
+ destroy_session ( req . env , options [ :id ] , options )
182
129
end
183
130
184
131
return [ status , headers , body ] if options [ :drop ] || options [ :skip ]
185
132
186
133
if loaded_session? ( session )
187
134
session_id = session . respond_to? ( :id= ) ? session . id : options [ :id ]
188
135
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." )
191
138
end
192
139
end
193
140
@@ -245,7 +192,6 @@ def destroy_session(env, session_id = nil, options = nil)
245
192
rescue java . lang . IllegalStateException # if session already invalid
246
193
nil
247
194
end
248
- alias :destroy :destroy_session # for AbstractStore::SessionHash compatibility
249
195
250
196
end
251
197
0 commit comments