Class: SBSM::Session
- Inherits:
-
Object
- Object
- SBSM::Session
- 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
- EXPIRES =
60 * 60
- LF_FACTORY =
nil
- LOOKANDFEEL =
Lookandfeel
- CAP_MAX_THRESHOLD =
8
- MAX_STATES =
4
- SERVER_NAME =
nil
- UNKNOWN_USER =
UnknownUser
- @@mutex =
Mutex.new
- @@async =
ThreadGroup.new
- @@stats_ptrn =
/./
- @@msie_ptrn =
/MSIE/
- @@win_ptrn =
/Win/i
- @@nt5_ptrn =
/Windows\s*NT\s*(\d+\.\d+)/i
- @@hash_ptrn =
should matches stuff like “hash”
/([^\[]+)((\[[^\]]+\])+)/
- @@index_ptrn =
/[^\[\]]+/
- @@input_ptrn =
/([^\[]+)\[([^\]]+)\]/
Instance Attribute Summary collapse
-
#active_thread ⇒ Object
readonly
Returns the value of attribute active_thread.
-
#app ⇒ Object
Returns the value of attribute app.
-
#attended_states ⇒ Object
readonly
Returns the value of attribute attended_states.
-
#cgi ⇒ Object
readonly
Returns the value of attribute cgi.
-
#cookie_input ⇒ Object
readonly
Returns the value of attribute cookie_input.
-
#key ⇒ Object
Returns the value of attribute key.
-
#persistent_cookie_name ⇒ Object
readonly
Returns the value of attribute persistent_cookie_name.
-
#post_content ⇒ Object
readonly
Returns the value of attribute post_content.
-
#rack_request ⇒ Object
Returns the value of attribute rack_request.
-
#request_method ⇒ Object
readonly
Returns the value of attribute request_method.
-
#request_origin ⇒ Object
readonly
Returns the value of attribute request_origin.
-
#request_params ⇒ Object
readonly
Returns the value of attribute request_params.
-
#request_path ⇒ Object
readonly
Returns the value of attribute request_path.
-
#request_post ⇒ Object
readonly
Returns the value of attribute request_post.
-
#server_name ⇒ Object
readonly
Returns the value of attribute server_name.
-
#server_port ⇒ Object
readonly
Returns the value of attribute server_port.
-
#trans_handler ⇒ Object
Returns the value of attribute trans_handler.
-
#unsafe_input ⇒ Object
readonly
Returns the value of attribute unsafe_input.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#user_agent ⇒ Object
readonly
Returns the value of attribute user_agent.
-
#valid_input ⇒ Object
readonly
Returns the value of attribute valid_input.
-
#validator ⇒ Object
Returns the value of attribute validator.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #__checkout ⇒ Object
- #age(now = Time.now) ⇒ Object
- #async(&block) ⇒ Object
- #cap_max_states ⇒ Object
- #client_activex? ⇒ Boolean
- #client_nt5? ⇒ Boolean
-
#close ⇒ Object
nothing.
- #cookie_pairs ⇒ Object
- #cookie_set_or_get(key) ⇒ Object
- #default_language ⇒ Object
-
#delete ⇒ Object
nothing.
- #direct_event ⇒ 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
- #get_passthru ⇒ Object
- #http_headers ⇒ Object
- #http_protocol ⇒ Object
- #import_cookies(request) ⇒ Object
- #import_user_input(rack_req) ⇒ Object
- #info? ⇒ Boolean
- #infos ⇒ Object
-
#initialize(app:, trans_handler: nil, validator: nil, unknown_user: SBSM::UnknownUser.new, cookie_name: nil, multi_threaded: false) ⇒ Session
constructor
Session: It will be initialized indirectly whenever SessionStore cannot find a session in it cache.
- #input_keys ⇒ Object
- #is_crawler? ⇒ Boolean
- #language ⇒ Object
- #logged_in? ⇒ Boolean
- #login ⇒ Object
- #logout ⇒ Object
- #lookandfeel ⇒ Object
-
#method_missing(symbol, *args, &block) ⇒ Object
Replaces old dispatch to DRb.
- #navigation ⇒ Object
- #passthru(path, disposition = 'attachment') ⇒ Object
- #persistent_user_input(key) ⇒ Object
- #process_rack(rack_request:) ⇒ Object
- #remote_addr ⇒ Object
- #remote_ip ⇒ Object
- #reset ⇒ Object
- #reset_cookie ⇒ Object
- #reset_input ⇒ Object
-
#restore ⇒ Object
CGI::SessionHandler compatibility ruby-doc.org/stdlib-2.3.1/libdoc/cgi/rdoc/CGI.html should restore the values from a file, we return simply nothing.
- #set_cookie_input(key, val) ⇒ Object
- #state(event = nil) ⇒ Object
- #to_html ⇒ Object
- #touch ⇒ Object
- #unknown_user ⇒ Object
- #update ⇒ Object
- #user_input(*keys) ⇒ Object
- #valid_values(key) ⇒ Object
- #warning? ⇒ Boolean
- #warnings ⇒ Object
- #zone ⇒ Object
- #zone_navigation ⇒ Object
- #zones ⇒ Object
Constructor Details
#initialize(app:, trans_handler: nil, validator: nil, unknown_user: SBSM::UnknownUser.new, cookie_name: nil, multi_threaded: false) ⇒ Session
Session: It will be initialized indirectly whenever SessionStore cannot find a session in it cache. The parameters are given via SBSM::App.new which calls SessionStore.new
optional arguments
-
validator
- A Ruby class overriding the SBSM::Validator class -
trans_handler
- A Ruby class overriding the SBSM::TransHandler class -
cookie_name
- The cookie to save persistent user data
Examples
Look at steinwies.ch
-
github.com/zdavatz/steinwies.ch (simple, mostly static files, one form, no persistence layer)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/sbsm/session.rb', line 109 def initialize(app:, trans_handler: nil, validator: nil, unknown_user: SBSM::UnknownUser.new, cookie_name: nil, multi_threaded: false) SBSM.info "initialize th #{trans_handler} validator #{validator} app #{app.class} multi_threaded #{multi_threaded}" @app = app @unknown_user = unknown_user.is_a?(Class) ? unknown_user.new : unknown_user @validator = validator if validator.is_a?(SBSM::Validator) @validator ||= (validator && validator.new) || Validator.new fail "invalid validator #{@validator}" unless @validator.is_a?(SBSM::Validator) @trans_handler = trans_handler || TransHandler.instance fail "invalid trans_handler #{@trans_handler}" unless @trans_handler.is_a?(SBSM::TransHandler) = ||= self.class::PERSISTENT_COOKIE_NAME @attended_states = {} @persistent_user_input = {} touch() reset_input() () @user = SBSM::UnknownUser.new @variables = {} @cgi = CGI.initialize_without_offline_prompt('html4') @multi_threaded = multi_threaded @mutex = multi_threaded ? Mutex.new: @@mutex @active_thread = nil SBSM.debug "session initialized #{self} with @cgi #{@cgi} multi_threaded #{multi_threaded} app #{app.object_id} and user #{@user.class} @unknown_user #{@unknown_user.class}" end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
Replaces old dispatch to DRb
138 139 140 141 142 143 144 |
# File 'lib/sbsm/session.rb', line 138 def method_missing(symbol, *args, &block) # Replaces old dispatch to DRb @app.send(symbol, *args, &block) rescue => error puts error puts error.backtrace.join("\n") raise error end |
Instance Attribute Details
#active_thread ⇒ Object (readonly)
Returns the value of attribute active_thread.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def active_thread @active_thread end |
#app ⇒ Object
Returns the value of attribute app.
43 44 45 |
# File 'lib/sbsm/session.rb', line 43 def app @app end |
#attended_states ⇒ Object (readonly)
Returns the value of attribute attended_states.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def attended_states @attended_states end |
#cgi ⇒ Object (readonly)
Returns the value of attribute cgi.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def cgi @cgi end |
#cookie_input ⇒ Object (readonly)
Returns the value of attribute cookie_input.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def end |
#key ⇒ Object
Returns the value of attribute key.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def key @key end |
#persistent_cookie_name ⇒ Object (readonly)
Returns the value of attribute persistent_cookie_name.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def end |
#post_content ⇒ Object (readonly)
Returns the value of attribute post_content.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def post_content @post_content end |
#rack_request ⇒ Object
Returns the value of attribute rack_request.
43 44 45 |
# File 'lib/sbsm/session.rb', line 43 def rack_request @rack_request end |
#request_method ⇒ Object (readonly)
Returns the value of attribute request_method.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def request_method @request_method end |
#request_origin ⇒ Object (readonly)
Returns the value of attribute request_origin.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def request_origin @request_origin end |
#request_params ⇒ Object (readonly)
Returns the value of attribute request_params.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def request_params @request_params end |
#request_path ⇒ Object (readonly)
Returns the value of attribute request_path.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def request_path @request_path end |
#request_post ⇒ Object (readonly)
Returns the value of attribute request_post.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def request_post @request_post end |
#server_name ⇒ Object (readonly)
Returns the value of attribute server_name.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def server_name @server_name end |
#server_port ⇒ Object (readonly)
Returns the value of attribute server_port.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def server_port @server_port end |
#trans_handler ⇒ Object
Returns the value of attribute trans_handler.
43 44 45 |
# File 'lib/sbsm/session.rb', line 43 def trans_handler @trans_handler end |
#unsafe_input ⇒ Object (readonly)
Returns the value of attribute unsafe_input.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def unsafe_input @unsafe_input end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def user @user end |
#user_agent ⇒ Object (readonly)
Returns the value of attribute user_agent.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def user_agent @user_agent end |
#valid_input ⇒ Object (readonly)
Returns the value of attribute valid_input.
40 41 42 |
# File 'lib/sbsm/session.rb', line 40 def valid_input @valid_input end |
#validator ⇒ Object
Returns the value of attribute validator.
43 44 45 |
# File 'lib/sbsm/session.rb', line 43 def validator @validator end |
Class Method Details
.reset_stats ⇒ Object
58 59 60 |
# File 'lib/sbsm/session.rb', line 58 def Session.reset_stats @@stats = {} end |
.show_stats(ptrn = @@stats_ptrn) ⇒ Object
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 90 91 92 93 |
# File 'lib/sbsm/session.rb', line 63 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
613 614 615 |
# File 'lib/sbsm/session.rb', line 613 def <=>(other) self.weighted_mtime <=> other.weighted_mtime end |
#==(other) ⇒ Object
610 611 612 |
# File 'lib/sbsm/session.rb', line 610 def ==(other) super end |
#[](key) ⇒ Object
616 617 618 |
# File 'lib/sbsm/session.rb', line 616 def [](key) @variables[key] end |
#[]=(key, val) ⇒ Object
619 620 621 |
# File 'lib/sbsm/session.rb', line 619 def []=(key, val) @variables[key] = val end |
#__checkout ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/sbsm/session.rb', line 167 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 true end |
#age(now = Time.now) ⇒ Object
150 151 152 |
# File 'lib/sbsm/session.rb', line 150 def age(now=Time.now) now - @mtime end |
#async(&block) ⇒ Object
622 623 624 |
# File 'lib/sbsm/session.rb', line 622 def async(&block) @@async.add(Thread.new(&block)) end |
#cap_max_states ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/sbsm/session.rb', line 153 def cap_max_states if(@attended_states.size > self::class::CAP_MAX_THRESHOLD) SBSM.info "too many states in session! Keeping only #{self::class::MAX_STATES}" 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 |
#client_activex? ⇒ Boolean
181 182 183 |
# File 'lib/sbsm/session.rb', line 181 def client_activex? (ua = user_agent) && @@msie_ptrn.match(ua) && @@win_ptrn.match(ua) end |
#client_nt5? ⇒ Boolean
185 186 187 188 189 |
# File 'lib/sbsm/session.rb', line 185 def client_nt5? (ua = user_agent) \ && (match = @@nt5_ptrn.match(user_agent)) \ && (match[1].to_f >= 5) end |
#close ⇒ Object
nothing
594 595 596 597 |
# File 'lib/sbsm/session.rb', line 594 def close #@unix_socket.stop_service # nothing end |
#cookie_pairs ⇒ Object
445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/sbsm/session.rb', line 445 def .collect do |key, value| if value if value.is_a?(String) "#{key}=#{CGI.escape(value)}" else "#{key}=#{value}" end else "#{key}=" end end.join(';') end |
#cookie_set_or_get(key) ⇒ Object
190 191 192 193 194 195 196 |
# File 'lib/sbsm/session.rb', line 190 def (key) if(value = @valid_input[key]) (key, value) else [key] end end |
#default_language ⇒ Object
200 201 202 |
# File 'lib/sbsm/session.rb', line 200 def default_language self::class::DEFAULT_LANGUAGE end |
#delete ⇒ Object
nothing
598 599 600 |
# File 'lib/sbsm/session.rb', line 598 def delete @app.delete_session(@key) end |
#direct_event ⇒ Object
203 204 205 206 |
# File 'lib/sbsm/session.rb', line 203 def direct_event # used when @state.direct_event end |
#error(key) ⇒ Object
284 285 286 |
# File 'lib/sbsm/session.rb', line 284 def error(key) @state.error(key) if @state.respond_to?(:error) end |
#error? ⇒ Boolean
290 291 292 |
# File 'lib/sbsm/session.rb', line 290 def error? @state.error? if @state.respond_to?(:error?) end |
#errors ⇒ Object
287 288 289 |
# File 'lib/sbsm/session.rb', line 287 def errors @state.errors.values if @state.respond_to?(:errors) end |
#event ⇒ Object
293 294 295 |
# File 'lib/sbsm/session.rb', line 293 def event @valid_input[:event] end |
#event_bound_user_input(key) ⇒ Object
296 297 298 299 300 301 302 303 304 305 |
# File 'lib/sbsm/session.rb', line 296 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
306 307 308 |
# File 'lib/sbsm/session.rb', line 306 def expired?(now=Time.now) age(now) > EXPIRES end |
#flavor ⇒ Object
433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/sbsm/session.rb', line 433 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
309 310 311 |
# File 'lib/sbsm/session.rb', line 309 def force_login(user) @user = user end |
#get_cookie_input(key) ⇒ Object
197 198 199 |
# File 'lib/sbsm/session.rb', line 197 def (key) [key] end |
#get_passthru ⇒ Object
477 478 479 |
# File 'lib/sbsm/session.rb', line 477 def get_passthru @passthru ? [@passthru, @disposition] : [] end |
#http_headers ⇒ Object
458 459 460 461 462 463 |
# File 'lib/sbsm/session.rb', line 458 def http_headers @state.http_headers rescue NameError, StandardError => err SBSM.info "NameError, StandardError: #@request_path" {'Content-Type' => 'text/plain'} end |
#http_protocol ⇒ Object
464 465 466 467 468 469 470 |
# File 'lib/sbsm/session.rb', line 464 def http_protocol @http_protocol ||= if(@server_port && @server_port.to_i == 443) || ENV['SERVER_PORT'] 'https' else 'http' end end |
#import_cookies(request) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/sbsm/session.rb', line 312 def (request) () if(cuki = request.[]) cuki.split(';').each { |cuki_str| CGI.parse(CGI.unescape(cuki_str)).each { |key, val| key = key.intern valid = @validator.validate(key, val.compact.last) .store(key, valid) } } end request..each do |key, value| next if key.to_s.eql?(.to_s) key = key.intern valid = @validator.validate(key, value) .store(key, valid) end if false SBSM.debug "@cookie_input now #{@cookie_input}" end |
#import_user_input(rack_req) ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 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 334 def import_user_input(rack_req) return if(@user_input_imported) hash = rack_req.env.merge rack_req.params hash.merge! rack_req.POST if rack_req.POST hash.delete('rack.request.form_hash') # SBSM.debug "hash has #{hash.size } items #{hash.keys}" hash.each do |key, value| next if /^rack\./.match(key) index = nil @unsafe_input.push([key.to_s.dup, value.to_s.dup]) unless(key.nil? || key.empty?) if value.is_a?(Hash) key_sym = key.to_sym if @validator.validate(key_sym, value) @valid_input[key_sym] ||= {} value.each{ |k, v| @valid_input[key_sym][k] = v } end next end # Next for if match = @@hash_ptrn.match(key) key = match[1] index = match[2] # puts "key #{key} index #{index} value #{value}" end key = key.intern if(key == :confirm_pass) pass = rack_req.params["pass"] SBSM.debug "pass:#{pass} - confirm:#{value}" @valid_input[key] = @valid_input[:set_pass] \ = @validator.set_pass(pass, value) else valid = @validator.validate(key, value) # SBSM.debug "Checking #{key} -> #{value} valid #{valid.inspect} index #{index.inspect}" 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 end end end end @user_input_imported = true end |
#info? ⇒ Boolean
392 393 394 |
# File 'lib/sbsm/session.rb', line 392 def info? @state.info? if @state.respond_to?(:info?) end |
#infos ⇒ Object
389 390 391 |
# File 'lib/sbsm/session.rb', line 389 def infos @state.infos if @state.respond_to?(:infos) end |
#input_keys ⇒ Object
471 472 473 |
# File 'lib/sbsm/session.rb', line 471 def input_keys @valid_input.keys end |
#is_crawler? ⇒ Boolean
395 396 397 398 |
# File 'lib/sbsm/session.rb', line 395 def is_crawler? crawler_pattern = /archiver|slurp|bot|crawler|jeeves|spider|\.{6}/i !!(@rack_request && crawler_pattern.match(@rack_request.user_agent)) end |
#language ⇒ Object
399 400 401 |
# File 'lib/sbsm/session.rb', line 399 def language (:language) || default_language end |
#logged_in? ⇒ Boolean
402 403 404 |
# File 'lib/sbsm/session.rb', line 402 def logged_in? !@user.is_a?(SBSM::UnknownUser) end |
#login ⇒ Object
405 406 407 408 409 410 411 412 |
# File 'lib/sbsm/session.rb', line 405 def login if(user = (@app && @app.respond_to?(:login) && @app.login(self))) SBSM.debug "user is #{user.class} #{request_path.inspect}" @user = user else SBSM.debug "login no user #{request_path.inspect}" end end |
#logout ⇒ Object
413 414 415 416 417 418 419 420 |
# File 'lib/sbsm/session.rb', line 413 def logout __checkout @user = @unknown_user @active_state = @state = self::class::DEFAULT_STATE.new(self, @user) SBSM.debug "logout #{request_path.inspect} setting @state #{@state.object_id} #{@state.class} remember #{persistent_user_input(:remember).inspect} #{@user.class}" @state.init @attended_states.store(@state.object_id, @state) end |
#lookandfeel ⇒ Object
421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/sbsm/session.rb', line 421 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
474 475 476 |
# File 'lib/sbsm/session.rb', line 474 def @user. end |
#passthru(path, disposition = 'attachment') ⇒ Object
480 481 482 483 484 485 |
# File 'lib/sbsm/session.rb', line 480 def passthru(path, disposition='attachment') # the variable @passthru is set by a trusted source @passthru = path @disposition = disposition '' end |
#persistent_user_input(key) ⇒ Object
486 487 488 489 490 491 492 |
# File 'lib/sbsm/session.rb', line 486 def persistent_user_input(key) if(value = user_input(key)) @persistent_user_input.store(key, value) else @persistent_user_input[key] end end |
#process_rack(rack_request:) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/sbsm/session.rb', line 207 def process_rack(rack_request:) start = Time.now html = @mutex.synchronize do begin @passthru = false @disposition = false @rack_request = rack_request @request_path ||= rack_request.path @post_content = nil if rack_request.request_method.eql?('POST') rack_request.params.each do |k, v| # needed to test POST requests generated by curl (first parameter) or ARC (second parameter) if /xml/i.match(k) @post_content = "#{k} #{v}" break end end begin # needed for request generated by https://github.com/wiztools/rest-client rack_request.body.rewind # just to be sure @post_content = rack_request.body.read end unless @post_content if @post_content SBSM.debug "@post_content is #{@post_content}" else SBSM.debug "rack_request is #{rack_request}" end end rack_request.params.each { |key, val| @cgi.params.store(key, val) } @trans_handler.translate_uri(rack_request) @request_method =rack_request.request_method @request_path = rack_request.path if rack_request.env @request_origin = 'http' if (proto = rack_request.env['SERVER_PROTOCOL']) @request_origin = proto.downcase.match(/^\w+/)[0] end @request_origin += '://' @request_origin += rack_request.env['REMOTE_ADDR'] if rack_request.env['REMOTE_ADDR'] @remote_ip = rack_request.env['HTTP_X_FORWARDED_FOR'] end @server_name = rack_request.env['SERVER_NAME'] @server_port = rack_request.env['SERVER_PORT'] @request_params = rack_request.params logout unless @active_state validator.reset_errors() if validator && validator.respond_to?(:reset_errors) (rack_request) import_user_input(rack_request) @state = active_state.trigger(event()) SBSM.debug "active_state.trigger state #{@state.object_id} #{@state.class} remember #{persistent_user_input(:remember).inspect}" #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? SBSM.debug "Changing from #{@active_state.object_id} to state #{@state.class} #{@state.object_id} remember #{persistent_user_input(:remember).inspect} #{@user.class}" @active_state = @state @attended_states.store(@state.object_id, @state) else SBSM.debug "Stay in volatile state #{@state.object_id} #{@state.class}" end @zone = @active_state.zone @active_state.touch cap_max_states self.process_late() if self.respond_to?(:process_late) # needed for ODDB.org limit_request ensure @user_input_imported = false end to_html end (@@stats[@request_path] ||= []).push(Time.now - start) html end |
#remote_addr ⇒ Object
511 512 513 514 515 |
# File 'lib/sbsm/session.rb', line 511 def remote_addr @remote_addr ||= if @request.respond_to?(:remote_addr) @request.remote_addr end end |
#remote_ip ⇒ Object
516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/sbsm/session.rb', line 516 def remote_ip @remote_ip ||= if(@request.respond_to?(:remote_host)) @request.remote_host end if @remote_ip.to_s.index(',') saved = @remote_ip.clone @remote_ip = @remote_ip.first if @remote_ip.is_a?(Array) @remote_ip = @remote_ip.split(',').first.gsub(/[\[\]]/, '') if @remote_ip.is_a?(String) SBSM.info("remote_ip is weird #{saved.inspect} => #{@remote_ip.inspect}") end @remote_ip end |
#reset ⇒ Object
493 494 495 496 497 498 499 500 |
# File 'lib/sbsm/session.rb', line 493 def reset if @redirected SBSM.debug "reached Session::reset" @redirected = false else reset_input() end end |
#reset_cookie ⇒ Object
501 502 503 |
# File 'lib/sbsm/session.rb', line 501 def = {} end |
#reset_input ⇒ Object
504 505 506 507 508 509 510 |
# File 'lib/sbsm/session.rb', line 504 def reset_input @valid_input = {} @processing_errors = {} @http_protocol = nil @flavor = nil @unsafe_input = [] end |
#restore ⇒ Object
CGI::SessionHandler compatibility ruby-doc.org/stdlib-2.3.1/libdoc/cgi/rdoc/CGI.html should restore the values from a file, we return simply nothing
588 589 590 |
# File 'lib/sbsm/session.rb', line 588 def restore {} end |
#set_cookie_input(key, val) ⇒ Object
528 529 530 531 |
# File 'lib/sbsm/session.rb', line 528 def (key, val) SBSM.debug"set_cookie_input #{key} #{val}" .store(key, val) end |
#state(event = nil) ⇒ Object
539 540 541 |
# File 'lib/sbsm/session.rb', line 539 def state(event=nil) @active_state end |
#to_html ⇒ Object
546 547 548 |
# File 'lib/sbsm/session.rb', line 546 def to_html @state.to_html(cgi) end |
#touch ⇒ Object
542 543 544 545 |
# File 'lib/sbsm/session.rb', line 542 def touch @mtime = Time.now self end |
#unknown_user ⇒ Object
145 146 147 148 149 |
# File 'lib/sbsm/session.rb', line 145 def unknown_user @unknown_user || SBSM::UnknownUser.new puts "unknown_user set to #{@unknown_user} class #{ @unknown_user.is_a?(Class)}" @unknown_user = @unknown_user.new if @unknown_user.is_a?(Class) end |
#update ⇒ Object
591 592 593 |
# File 'lib/sbsm/session.rb', line 591 def update # nothing end |
#user_input(*keys) ⇒ Object
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 |
# File 'lib/sbsm/session.rb', line 553 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
575 576 577 578 |
# File 'lib/sbsm/session.rb', line 575 def valid_values(key) vals = @validator.valid_values(key) unless @validator.nil? vals || [] end |
#warning? ⇒ Boolean
582 583 584 |
# File 'lib/sbsm/session.rb', line 582 def warning? @state.warning? if @state.respond_to?(:warning?) end |
#warnings ⇒ Object
579 580 581 |
# File 'lib/sbsm/session.rb', line 579 def warnings @state.warnings if @state.respond_to?(:warnings) end |
#zone ⇒ Object
601 602 603 |
# File 'lib/sbsm/session.rb', line 601 def zone @valid_input[:zone] || (@state && @state.zone) || self::class::DEFAULT_ZONE end |
#zone_navigation ⇒ Object
607 608 609 |
# File 'lib/sbsm/session.rb', line 607 def @state. if @state end |
#zones ⇒ Object
604 605 606 |
# File 'lib/sbsm/session.rb', line 604 def zones @active_state.zones end |