Class: SBSM::Session
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- SBSM::Session
- Includes:
- DRbUndumped
- Defined in:
- lib/sbsm/session.rb
Constant Summary collapse
- PERSISTENT_COOKIE_NAME =
"sbsm-persistent-cookie"
- DEFAULT_FLAVOR =
'sbsm'
- DEFAULT_LANGUAGE =
'en'
- DEFAULT_STATE =
State
- DEFAULT_ZONE =
nil
- DRB_LOAD_LIMIT =
255 * 102400
- EXPIRES =
60 * 60
- LF_FACTORY =
nil
- LOOKANDFEEL =
Lookandfeel
- CAP_MAX_THRESHOLD =
8
- MAX_STATES =
4
- SERVER_NAME =
nil
- @@cgi =
CGI.new('html4')
- @@stats_ptrn =
/./
- @@msie_ptrn =
/MSIE/
- @@win_ptrn =
/Win/i
- @@nt5_ptrn =
/Windows\s*NT\s*(\d+\.\d+)/i
- @@hash_ptrn =
/([^\[]+)((\[[^\]]+\])+)/
- @@index_ptrn =
/[^\[\]]+/
- @@input_ptrn =
/([^\[]+)\[([^\]]+)\]/
Instance Attribute Summary collapse
-
#active_thread ⇒ Object
readonly
Returns the value of attribute active_thread.
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#cookie_input ⇒ Object
readonly
Returns the value of attribute cookie_input.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#request_path ⇒ Object
readonly
Returns the value of attribute request_path.
-
#unsafe_input ⇒ Object
readonly
Returns the value of attribute unsafe_input.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#valid_input ⇒ Object
readonly
Returns the value of attribute valid_input.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #__checkout ⇒ Object
- #age(now = Time.now) ⇒ Object
- #cap_max_states ⇒ Object
- #cgi ⇒ Object
- #client_activex? ⇒ Boolean
- #client_nt5? ⇒ Boolean
-
#close ⇒ Object
nothing.
- #cookie_name ⇒ Object
- #cookie_set_or_get(key) ⇒ Object
- #default_language ⇒ Object
-
#delete ⇒ Object
nothing.
- #direct_event ⇒ Object
- #drb_process(request) ⇒ Object
- #error(key) ⇒ Object
- #error? ⇒ Boolean
- #errors ⇒ Object
- #event ⇒ Object
- #event_bound_user_input(key) ⇒ Object
- #expired?(now = Time.now) ⇒ Boolean
- #flavor ⇒ Object
- #force_login(user) ⇒ Object
- #get_cookie_input(key) ⇒ Object
- #http_headers ⇒ Object
- #http_protocol ⇒ Object
- #import_cookies(request) ⇒ Object
- #import_user_input(request) ⇒ Object
- #info? ⇒ Boolean
-
#infos ⇒ Object
puts @unsafe_input.inspect puts @valid_input.inspect $stdout.flush.
-
#initialize(key, app, validator = nil) ⇒ Session
constructor
A new instance of Session.
- #input_keys ⇒ Object
- #is_crawler? ⇒ Boolean
- #language ⇒ Object
- #logged_in? ⇒ Boolean
- #login ⇒ Object
- #logout ⇒ Object
- #lookandfeel ⇒ Object
- #navigation ⇒ Object
- #passthru(*args) ⇒ Object
- #persistent_user_input(key) ⇒ Object
- #process(request) ⇒ Object
- #remote_addr ⇒ Object
- #remote_ip ⇒ Object
- #reset ⇒ Object
- #reset_cookie ⇒ Object
- #reset_input ⇒ Object
-
#restore ⇒ Object
CGI::SessionHandler compatibility.
- #server_name ⇒ Object
- #set_cookie_input(key, val) ⇒ Object
- #state(event = nil) ⇒ Object
- #to_html ⇒ Object
- #touch ⇒ Object
- #update ⇒ Object
- #user_agent ⇒ Object
- #user_input(*keys) ⇒ Object
- #valid_values(key) ⇒ Object
- #warning? ⇒ Boolean
- #warnings ⇒ Object
- #zone ⇒ Object
- #zone_navigation ⇒ Object
- #zones ⇒ Object
Constructor Details
#initialize(key, app, validator = nil) ⇒ Session
Returns a new instance of Session.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/sbsm/session.rb', line 90 def initialize(key, app, validator=nil) touch() reset_input() () @app = app @key = key @validator = validator @attended_states = {} @persistent_user_input = {} logout() @unknown_user_class = @user.class @variables = {} @mutex = Mutex.new #ARGV.push('') # satisfy cgi-offline prompt #@cgi = CGI.new('html4') super(app) end |
Instance Attribute Details
#active_thread ⇒ Object (readonly)
Returns the value of attribute active_thread.
38 39 40 |
# File 'lib/sbsm/session.rb', line 38 def active_thread @active_thread end |
#app ⇒ Object (readonly)
Returns the value of attribute app.
38 39 40 |
# File 'lib/sbsm/session.rb', line 38 def app @app end |
#cookie_input ⇒ Object (readonly)
Returns the value of attribute cookie_input.
38 39 40 |
# File 'lib/sbsm/session.rb', line 38 def @cookie_input end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
38 39 40 |
# File 'lib/sbsm/session.rb', line 38 def key @key end |
#request_path ⇒ Object (readonly)
Returns the value of attribute request_path.
38 39 40 |
# File 'lib/sbsm/session.rb', line 38 def request_path @request_path end |
#unsafe_input ⇒ Object (readonly)
Returns the value of attribute unsafe_input.
38 39 40 |
# File 'lib/sbsm/session.rb', line 38 def unsafe_input @unsafe_input end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
38 39 40 |
# File 'lib/sbsm/session.rb', line 38 def user @user end |
#valid_input ⇒ Object (readonly)
Returns the value of attribute valid_input.
38 39 40 |
# File 'lib/sbsm/session.rb', line 38 def valid_input @valid_input end |
Class Method Details
.reset_stats ⇒ Object
54 55 56 |
# File 'lib/sbsm/session.rb', line 54 def Session.reset_stats @@stats = {} end |
.show_stats(ptrn = @@stats_ptrn) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sbsm/session.rb', line 59 def Session.show_stats ptrn=@@stats_ptrn if ptrn.is_a?(String) ptrn = /#{ptrn}/i end puts sprintf("%8s %8s %8s %6s %10s Request-Path", "Min", "Max", "Avg", "Num", "Total") grand_total = requests = all_max = all_min = 0 @@stats.collect do |path, times| total = times.inject do |a, b| a + b end grand_total += total size = times.size requests += size max = times.max all_max = max > all_max ? max : all_max min = times.min all_min = min < all_min ? min : all_min [min, max, total / size, size, total, path] end.sort.each do |data| line = sprintf("%8.2f %8.2f %8.2f %6i %10.2f %s", *data) if ptrn.match(line) puts line end end puts sprintf("%8s %8s %8s %6s %10s Request-Path", "Min", "Max", "Avg", "Num", "Total") puts sprintf("%8.2f %8.2f %8.2f %6i %10.2f", all_min, all_max, requests > 0 ? grand_total / requests : 0, requests, grand_total) '' end |
Instance Method Details
#<=>(other) ⇒ Object
526 527 528 |
# File 'lib/sbsm/session.rb', line 526 def <=>(other) self.weighted_mtime <=> other.weighted_mtime end |
#==(other) ⇒ Object
523 524 525 |
# File 'lib/sbsm/session.rb', line 523 def ==(other) super end |
#[](key) ⇒ Object
529 530 531 |
# File 'lib/sbsm/session.rb', line 529 def [](key) @variables[key] end |
#[]=(key, val) ⇒ Object
532 533 534 |
# File 'lib/sbsm/session.rb', line 532 def []=(key, val) @variables[key] = val end |
#__checkout ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/sbsm/session.rb', line 122 def __checkout @attended_states.each_value { |state| state.__checkout } @attended_states.clear flavor = @persistent_user_input[:flavor] lang = @persistent_user_input[:language] @persistent_user_input.clear @persistent_user_input.store(:flavor, flavor) @persistent_user_input.store(:language, lang) @valid_input.clear @unsafe_input.clear @active_thread = nil true end |
#age(now = Time.now) ⇒ Object
107 108 109 |
# File 'lib/sbsm/session.rb', line 107 def age(now=Time.now) now - @mtime end |
#cap_max_states ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/sbsm/session.rb', line 110 def cap_max_states if(@attended_states.size > self::class::CAP_MAX_THRESHOLD) #puts "too many states in session! Keeping only #{self::class::MAX_STATES}" #$stdout.flush sorted = @attended_states.values.sort sorted[0...(-self::class::MAX_STATES)].each { |state| state.__checkout @attended_states.delete(state.object_id) } @attended_states.size end end |
#cgi ⇒ Object
135 136 137 |
# File 'lib/sbsm/session.rb', line 135 def cgi @@cgi end |
#client_activex? ⇒ Boolean
140 141 142 |
# File 'lib/sbsm/session.rb', line 140 def client_activex? (ua = user_agent) && @@msie_ptrn.match(ua) && @@win_ptrn.match(ua) end |
#client_nt5? ⇒ Boolean
144 145 146 147 148 |
# File 'lib/sbsm/session.rb', line 144 def client_nt5? (ua = user_agent) \ && (match = @@nt5_ptrn.match(user_agent)) \ && (match[1].to_f >= 5) end |
#close ⇒ Object
nothing
507 508 509 510 |
# File 'lib/sbsm/session.rb', line 507 def close #@unix_socket.stop_service # nothing end |
#cookie_name ⇒ Object
159 160 161 |
# File 'lib/sbsm/session.rb', line 159 def self::class::PERSISTENT_COOKIE_NAME end |
#cookie_set_or_get(key) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/sbsm/session.rb', line 149 def (key) if(value = @valid_input[key]) (key, value) else @cookie_input[key] end end |
#default_language ⇒ Object
162 163 164 |
# File 'lib/sbsm/session.rb', line 162 def default_language self::class::DEFAULT_LANGUAGE end |
#delete ⇒ Object
nothing
511 512 513 |
# File 'lib/sbsm/session.rb', line 511 def delete @app.delete_session @key end |
#direct_event ⇒ Object
165 166 167 |
# File 'lib/sbsm/session.rb', line 165 def direct_event @state.direct_event end |
#drb_process(request) ⇒ Object
168 169 170 171 172 173 174 175 176 |
# File 'lib/sbsm/session.rb', line 168 def drb_process(request) start = Time.now html = @mutex.synchronize do process(request) to_html end (@@stats[@request_path] ||= []).push(Time.now - start) html end |
#error(key) ⇒ Object
177 178 179 |
# File 'lib/sbsm/session.rb', line 177 def error(key) @state.error(key) if @state.respond_to?(:error) end |
#error? ⇒ Boolean
183 184 185 |
# File 'lib/sbsm/session.rb', line 183 def error? @state.error? if @state.respond_to?(:error?) end |
#errors ⇒ Object
180 181 182 |
# File 'lib/sbsm/session.rb', line 180 def errors @state.errors.values if @state.respond_to?(:errors) end |
#event ⇒ Object
186 187 188 |
# File 'lib/sbsm/session.rb', line 186 def event @valid_input[:event] end |
#event_bound_user_input(key) ⇒ Object
189 190 191 192 193 194 195 196 197 198 |
# File 'lib/sbsm/session.rb', line 189 def event_bound_user_input(key) @event_user_input ||= {} evt = state.direct_event @event_user_input[evt] ||= {} if(val = user_input(key)) @event_user_input[evt][key] = val else @event_user_input[evt][key] end end |
#expired?(now = Time.now) ⇒ Boolean
199 200 201 |
# File 'lib/sbsm/session.rb', line 199 def expired?(now=Time.now) age(now) > EXPIRES end |
#flavor ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/sbsm/session.rb', line 307 def flavor @flavor ||= begin user_input = persistent_user_input(:flavor) user_input ||= @valid_input[:default_flavor] lf_factory = self::class::LF_FACTORY if(lf_factory && lf_factory.include?(user_input)) user_input else self::class::DEFAULT_FLAVOR end end end |
#force_login(user) ⇒ Object
202 203 204 |
# File 'lib/sbsm/session.rb', line 202 def force_login(user) @user = user end |
#get_cookie_input(key) ⇒ Object
156 157 158 |
# File 'lib/sbsm/session.rb', line 156 def (key) @cookie_input[key] end |
#http_headers ⇒ Object
319 320 321 322 323 324 325 326 327 328 |
# File 'lib/sbsm/session.rb', line 319 def http_headers @state.http_headers rescue DRb::DRbConnError raise rescue NameError, StandardError => err puts "error in SBSM::Session#http_headers: #@request_path" puts err.class, err. puts err.backtrace[0,5] {'Content-Type' => 'text/plain'} end |
#http_protocol ⇒ Object
329 330 331 332 333 334 335 336 |
# File 'lib/sbsm/session.rb', line 329 def http_protocol @http_protocol ||= if(@request.respond_to?(:server_port) \ && @request.server_port == 443) 'https' else 'http' end end |
#import_cookies(request) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/sbsm/session.rb', line 205 def (request) () return if request..is_a?(DRb::DRbUnknown) if(cuki = request.[self::class::PERSISTENT_COOKIE_NAME]) cuki.each { |cuki_str| CGI.parse(CGI.unescape(cuki_str)).each { |key, val| key = key.intern valid = @validator.validate(key, val.compact.last) @cookie_input.store(key, valid) } } end end |
#import_user_input(request) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/sbsm/session.rb', line 220 def import_user_input(request) # attempting to read the cgi-params more than once results in a # DRbConnectionRefused Exception. Therefore, do it only once... return if(@user_input_imported) request.params.each { |key, value| #puts "importing #{key} -> #{value}" index = nil @unsafe_input.push([key.to_s.dup, value.to_s.dup]) unless(key.nil? || key.empty?) if match = @@hash_ptrn.match(key) key = match[1] index = match[2] #puts key, index end key = key.intern if(key == :confirm_pass) pass = request.params["pass"] #puts "pass:#{pass} - confirm:#{value}" @valid_input[key] = @valid_input[:set_pass] \ = @validator.set_pass(pass, value) else valid = @validator.validate(key, value) if(index) target = (@valid_input[key] ||= {}) indices = [] index.scan(@@index_ptrn) { |idx| indices.push idx } last = indices.pop indices.each { |idx| target = (target[idx] ||= {}) } target.store(last, valid) else @valid_input[key] = valid "dummy" # Some statement is necessary here to avoid Date data loading error on Ruby 1.9.3 end end end #puts "imported #{key} -> #{value} => #{@valid_input[key].inspect}" } @user_input_imported = true #puts @unsafe_input.inspect #puts @valid_input.inspect #$stdout.flush end |
#info? ⇒ Boolean
269 270 271 |
# File 'lib/sbsm/session.rb', line 269 def info? @state.info? if @state.respond_to?(:info?) end |
#infos ⇒ Object
puts @unsafe_input.inspect puts @valid_input.inspect $stdout.flush
266 267 268 |
# File 'lib/sbsm/session.rb', line 266 def infos @state.infos if @state.respond_to?(:infos) end |
#input_keys ⇒ Object
337 338 339 |
# File 'lib/sbsm/session.rb', line 337 def input_keys @valid_input.keys end |
#is_crawler? ⇒ Boolean
272 273 274 275 276 |
# File 'lib/sbsm/session.rb', line 272 def is_crawler? @is_crawler ||= if @request.respond_to?(:is_crawler?) @request.is_crawler? end end |
#language ⇒ Object
277 278 279 |
# File 'lib/sbsm/session.rb', line 277 def language (:language) || default_language end |
#logged_in? ⇒ Boolean
280 281 282 |
# File 'lib/sbsm/session.rb', line 280 def logged_in? !@user.is_a?(@unknown_user_class) end |
#login ⇒ Object
283 284 285 286 287 |
# File 'lib/sbsm/session.rb', line 283 def login if(user = @app.login(self)) @user = user end end |
#logout ⇒ Object
288 289 290 291 292 293 294 |
# File 'lib/sbsm/session.rb', line 288 def logout __checkout @user = @app.unknown_user() @active_state = @state = self::class::DEFAULT_STATE.new(self, @user) @state.init @attended_states.store(@state.object_id, @state) end |
#lookandfeel ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/sbsm/session.rb', line 295 def lookandfeel if(@lookandfeel.nil? \ || (@lookandfeel.flavor != flavor) \ || (@lookandfeel.language != persistent_user_input(:language))) @lookandfeel = if self::class::LF_FACTORY self::class::LF_FACTORY.create(self) else self::class::LOOKANDFEEL.new(self) end end @lookandfeel end |
#navigation ⇒ Object
340 341 342 |
# File 'lib/sbsm/session.rb', line 340 def @user. end |
#passthru(*args) ⇒ Object
343 344 345 |
# File 'lib/sbsm/session.rb', line 343 def passthru(*args) @request.passthru(*args) end |
#persistent_user_input(key) ⇒ Object
346 347 348 349 350 351 352 |
# File 'lib/sbsm/session.rb', line 346 def persistent_user_input(key) if(value = user_input(key)) @persistent_user_input.store(key, value) else @persistent_user_input[key] end end |
#process(request) ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/sbsm/session.rb', line 353 def process(request) begin @request = request @request_method = request.request_method @request_path = @request.unparsed_uri @validator.reset_errors() if @validator import_user_input(request) (request) @state = active_state.trigger(event()) #FIXME: is there a better way to distinguish returning states? # ... we could simply refuse to init if event == :sort, but that # would not solve the problem cleanly, I think. unless(@state.request_path) @state.request_path = @request_path @state.init end unless @state.volatile? @active_state = @state @attended_states.store(@state.object_id, @state) end @zone = @active_state.zone @active_state.touch cap_max_states rescue DRb::DRbConnError raise rescue StandardError => err #@state = @state.previous puts "error in SBSM::Session#process: #@request_path" puts err.class, err. puts err.backtrace[0,5] $stdout.flush ensure @user_input_imported = false end '' end |
#remote_addr ⇒ Object
417 418 419 420 421 |
# File 'lib/sbsm/session.rb', line 417 def remote_addr @remote_addr ||= if @request.respond_to?(:remote_addr) @request.remote_addr end end |
#remote_ip ⇒ Object
422 423 424 425 426 |
# File 'lib/sbsm/session.rb', line 422 def remote_ip @remote_ip ||= if(@request.respond_to?(:remote_host)) @request.remote_host end end |
#reset ⇒ Object
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/sbsm/session.rb', line 389 def reset =begin if(@active_thread \ && @active_thread.alive? \ && @active_thread != Thread.current) begin #@active_thread.exit rescue StandardError end end @active_thread = Thread.current =end if @redirected @redirected = false else reset_input() end end |
#reset_cookie ⇒ Object
407 408 409 |
# File 'lib/sbsm/session.rb', line 407 def @cookie_input = {} end |
#reset_input ⇒ Object
410 411 412 413 414 415 416 |
# File 'lib/sbsm/session.rb', line 410 def reset_input @valid_input = {} @processing_errors = {} @http_protocol = nil @flavor = nil @unsafe_input = [] end |
#restore ⇒ Object
CGI::SessionHandler compatibility
494 495 496 497 498 499 500 501 502 503 |
# File 'lib/sbsm/session.rb', line 494 def restore #puts "restore was called" #@unix_socket = DRb.start_service('drbunix:', self) hash = { #:proxy => DRbObject.new(self, @unix_socket.uri) :proxy => self, } hash.extend(DRbUndumped) # added for Ruby1.8 compliance hash end |
#server_name ⇒ Object
430 431 432 433 434 435 436 437 438 |
# File 'lib/sbsm/session.rb', line 430 def server_name @server_name ||= if @request.respond_to?(:server_name) @request.server_name else self::class::SERVER_NAME end rescue DRb::DRbConnError @server_name = self::class::SERVER_NAME end |
#set_cookie_input(key, val) ⇒ Object
427 428 429 |
# File 'lib/sbsm/session.rb', line 427 def (key, val) @cookie_input.store(key, val) end |
#state(event = nil) ⇒ Object
439 440 441 |
# File 'lib/sbsm/session.rb', line 439 def state(event=nil) @active_state end |
#to_html ⇒ Object
446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/sbsm/session.rb', line 446 def to_html @state.to_html(@@cgi) rescue DRb::DRbConnError raise rescue StandardError => err puts "error in SBSM::Session#to_html: #@request_path" puts err.class, err. puts err.backtrace#[0,5] $stdout.flush [ err.class, err. ].join("\n") end |
#touch ⇒ Object
442 443 444 445 |
# File 'lib/sbsm/session.rb', line 442 def touch @mtime = Time.now self end |
#update ⇒ Object
504 505 506 |
# File 'lib/sbsm/session.rb', line 504 def update # nothing end |
#user_agent ⇒ Object
457 458 459 |
# File 'lib/sbsm/session.rb', line 457 def user_agent @user_agent ||= (@request.user_agent if @request.respond_to?(:user_agent)) end |
#user_input(*keys) ⇒ Object
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/sbsm/session.rb', line 461 def user_input(*keys) if(keys.size == 1) index = nil key = keys.first.to_s if match = @@input_ptrn.match(key) key = match[1] index = match[2] end key_sym = key.to_sym valid = @valid_input[key_sym] if(index && valid.respond_to?(:[])) valid[index] else valid end else keys.inject({}) { |inj, key| inj.store(key, user_input(key)) inj } end end |
#valid_values(key) ⇒ Object
483 484 485 486 |
# File 'lib/sbsm/session.rb', line 483 def valid_values(key) vals = @validator.valid_values(key) unless @validator.nil? vals || [] end |
#warning? ⇒ Boolean
490 491 492 |
# File 'lib/sbsm/session.rb', line 490 def warning? @state.warning? if @state.respond_to?(:warning?) end |
#warnings ⇒ Object
487 488 489 |
# File 'lib/sbsm/session.rb', line 487 def warnings @state.warnings if @state.respond_to?(:warnings) end |
#zone ⇒ Object
514 515 516 |
# File 'lib/sbsm/session.rb', line 514 def zone @valid_input[:zone] || @state.zone || self::class::DEFAULT_ZONE end |
#zone_navigation ⇒ Object
520 521 522 |
# File 'lib/sbsm/session.rb', line 520 def @state. end |
#zones ⇒ Object
517 518 519 |
# File 'lib/sbsm/session.rb', line 517 def zones @active_state.zones end |