28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
|
# File 'lib/isomorfeus/transport/rack_middleware.rb', line 28
def call(env)
case env['PATH_INFO']
when Isomorfeus.api_websocket_path
if env['rack.upgrade?'] == :websocket
user, session_id = user_from_env(env)
env['rack.upgrade'] = Isomorfeus::Transport::ServerSocketProcessor.new(user, session_id)
end
WS_RESPONSE
when Isomorfeus.cookie_eater_path
cookie_accessor, new_path = env['QUERY_STRING'].split('=')
cookie = Isomorfeus.session_class&.take_cookie(accessor: cookie_accessor)
if new_path.start_with?('/')
if cookie
[302, { 'Location' => new_path, 'Set-Cookie' => cookie }, ["Cookie eaten!"]]
else
[302, { 'Location' => new_path }, ["No Cookie!"]]
end
else
[404, {}, ["Must specify relative path!"]]
end
when Isomorfeus.api_logout_path
user, session_id = user_from_env(env)
if user
begin
Isomorfeus.session_class&.remove(session_id: session_id)
cookie = "session=#{session_id}; SameSite=Strict; HttpOnly; Path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC#{'; Secure' if Isomorfeus.production?}"
return [302, { 'Location' => '/', 'Set-Cookie' => cookie }, ["Logged out!"]]
ensure
Thread.current[:isomorfeus_user] = nil
Thread.current[:isomorfeus_session_id] = nil
end
end
return [302, { 'Location' => '/', 'Set-Cookie' => cookie }, ["Tried to log out!"]]
else
user, session_id = user_from_env(env)
if user
Thread.current[:isomorfeus_user] = user
Thread.current[:isomorfeus_session_id] = session_id
end
begin
result = @app.call(env)
ensure
Thread.current[:isomorfeus_user] = nil
Thread.current[:isomorfeus_session_id] = nil
end
result
end
rescue Exception => e
e_text = "#{e.class}: #{e.message}\n #{e.backtrace.join("\n")}"
STDERR.puts e_text
message = if Isomorfeus.production?
"<html><head><title>Error</title></head><body>Sorry, a error occured!</body></html>"
else
"<html><head><title>#{e.class}</title></head><body><pre>#{e_text}</pre></body></html>"
end
return [500, { Rack::CONTENT_TYPE => "text/html", Rack::CONTENT_LENGTH => message.length.to_s }, [message]]
end
|