Skip to content

Commit

Permalink
Reworking session handling. Only half done
Browse files Browse the repository at this point in the history
  • Loading branch information
ngiger committed Oct 25, 2016
1 parent 7b422d2 commit 1d8db5a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
15 changes: 9 additions & 6 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ DRb.start_service('druby://localhost:0')
name = File.basename(File.dirname(__FILE__))
logname = File.expand_path(File.join(File.dirname(__FILE__), "log/access.log"))
FileUtils.makedirs(File.dirname(logname))
Rack::Server.start(
:app => Rack::ShowExceptions.new(Rack::Lint.new(Steinwies::App.new)),
:Port => 8004
app = Rack::ShowExceptions.new(Rack::Lint.new(Steinwies::App.new))
# app = Rack::ShowExceptions.new(Steinwies::App.new)
sessioned = Rack::Session::Pool.new(app,
:domain => 'foo.com',
:expire_after => 2592000
)

# :Logger => WEBrick::Log.new(logname, WEBrick::Log::INFO),
# :AccessLog => [[File.open(logname,'w'),WEBrick::AccessLog::COMBINED_LOG_FORMAT]]
Rack::Server.start( :app => sessioned,
:Port => 8004,
)
Rack::Handler::WEBrick.run sessioned, {:Port => 8004} if false
49 changes: 46 additions & 3 deletions src/util/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,57 @@
require 'util/session'
require 'util/trans_handler.steinwies'
require 'util/config'
require 'pry'

module Steinwies
class App < SBSM::DRbServer
SESSION = Session
attr_accessor :session_pool
@@counter = 0
COOKIE_ID = 'sbsm-persistent-cookie-id'
def initialize
@app = self
@session_pool = Rack::Session::Pool.new(@app, :domain => 'foo.com', :expire_after => 2592000 )
@sbsm = SBSM::Request.new(Steinwies.config.server_uri, Steinwies::TransHandler.instance, {})
@validator = Steinwies::Validator.new
end
def call(env)
sbsm = SBSM::Request.new(Steinwies.config.server_uri, Steinwies::TransHandler.instance, env)
response = sbsm.process
response.finish
@sbsm = SBSM::Request.new(Steinwies.config.server_uri, Steinwies::TransHandler.instance, {})
@sbsm.request = Rack::Request.new(env)
@sbsm.session = Rack::Session::Abstract::SessionHash.find(@sbsm.request)
@sbsm.request.session.merge! env
puts "call env #{env.keys} cookie_id #{@sbsm.request.cookies[COOKIE_ID]}"
session = SESSION.new(@sbsm.request.cookies[COOKIE_ID], self, @validator)
@sbsm.request.session[:proxy] = session
response = @sbsm.process
puts response.class
puts "res1 #{response.status} with length #{response.body.join('').length} headers #{response.headers}"
response.headers.delete('Content-Length')
puts "res2 #{response.status} with length #{response.body.join('').length} headers #{response.headers}"
return response.finish
if false
puts "cookies are #{request.cookies}"
puts "_session_id is #{request.cookies[COOKIE_ID]} or #{request.session.id.inspect}"
if /favicon.ico/i.match(request.path)
return [400, {}, []]
end
saved = request.cookies['old_path']
response = Rack::Response.new [
"Hello World\n",
"ID: #{request.cookies[COOKIE_ID] ? request.cookies[COOKIE_ID] : 'neue Session' }\n",
"counter #{@@counter}\n",
"We moved from #{saved}. now at #{request.path}"], 200, {}

session = Rack::Session::Abstract::SessionHash.find(request)
puts "found session #{session}"
@@counter += 1
[200, {}, ["Hello World <br> We moved from #{saved}. now at #{request.path}"]]

response.set_cookie(COOKIE_ID, {:value => request.cookies[COOKIE_ID], :path => "/", :expires => Time.now+24*60*60})
response.set_cookie('@@counter', {:value => @@counter, :path => "/", :expires => Time.now+24*60*60})
response.set_cookie('old_path', {:value => request.path, :path => "/", :expires => Time.now+24*60*60})
response.finish
end
end
end
end
10 changes: 7 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ module Steinwies::TestCase

require 'util/app'
require 'rack/test'
OUTER_APP = Rack::Builder.parse_file('config.ru').first

class SteinwiesTest < Minitest::Test
include Rack::Test::Methods
@drb_server = nil
def app
@app = Steinwies::App.new
OUTER_APP
end
def setup
puts "setup starting"
@drb = Thread.new do
begin
DRb.stop_service
Expand All @@ -75,10 +77,12 @@ def setup
# Wait for service to be ready, or the tests will fail with DRb-errors
until @drb_server && @drb_server.alive? do sleep 0.005 end
@drb.abort_on_exception = false
end
puts "setup done"
end if false
def teardown
# @drb.exit
DRb.stop_service
@drb_server = nil
end
puts "teardown starting"
end if false
end

0 comments on commit 1d8db5a

Please sign in to comment.