Class: Isomorfeus::Transport::RackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/isomorfeus/transport/rack_middleware.rb

Constant Summary collapse

WS_RESPONSE =
[0, {}, []]

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.



8
9
10
# File 'lib/isomorfeus/transport/rack_middleware.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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

#user_from_env(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/isomorfeus/transport/rack_middleware.rb', line 12

def user_from_env(env)
  cookies = env['HTTP_COOKIE']
  if cookies
    cookies = cookies.split('; ')
    cookie = cookies.detect { |c| c.start_with?('session=') }
    if cookie
      session_id = cookie[8..-1]
      if !session_id.nil? && !session_id.empty?
        user = Isomorfeus.session_class&.get_user(session_id: session_id)
        return [user, session_id] unless user.nil?
      end
    end
  end
  [Anonymous.new, nil]
end