Sha256: a3310aa3532052e87ff03655504bef894c433bc83e19bc1727747e1431ded327

Contents?: true

Size: 1.87 KB

Versions: 11

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module Isomorfeus
  module Transport
    class RackMiddleware
      WS_RESPONSE = [0, {}, []]

      def initialize(app)
        @app = app
      end

      def call(env)
        if env['PATH_INFO'] == Isomorfeus.api_websocket_path
          if env['rack.upgrade?'] == :websocket
            env['rack.upgrade'] = Isomorfeus::Transport::ServerSocketProcessor.new
          end
          WS_RESPONSE
        elsif env['PATH_INFO'] == Isomorfeus.cookie_eater_path
          cookie_accessor, new_path = env['QUERY_STRING'].split('=')
          cookie = Isomorfeus.session_store.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
        else
          cookies = env['HTTP_COOKIE']
          if cookies
            cookies = cookies.split('; ')
            cookie = cookies.detect { |c| c.start_with?('session=') }
            if cookie
              session_id = cookie[8..-1]
              user = Isomorfeus.session_store.get_user(session_id: session_id)
              if user
                Thread.current[:isomorfeus_user] = user
                Thread.current[:isomorfeus_session_id] = session_id
              end
            end
          end
          begin
            result = @app.call(env)
          ensure
            Thread.current[:isomorfeus_user] = nil
            Thread.current[:isomorfeus_session_id] = nil
          end
          result
        end
      rescue Exception => e
        # TODO
        Isomorfeus.raise_error(error: e)
        return [500, {}, '']
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
isomorfeus-transport-2.0.0 lib/isomorfeus/transport/rack_middleware.rb
isomorfeus-transport-2.0.0.rc10 lib/isomorfeus/transport/rack_middleware.rb
isomorfeus-transport-2.0.0.rc9 lib/isomorfeus/transport/rack_middleware.rb
isomorfeus-transport-2.0.0.rc8 lib/isomorfeus/transport/rack_middleware.rb
isomorfeus-transport-2.0.0.rc7 lib/isomorfeus/transport/rack_middleware.rb
isomorfeus-transport-2.0.0.rc6 lib/isomorfeus/transport/rack_middleware.rb
isomorfeus-transport-2.0.0.rc5 lib/isomorfeus/transport/rack_middleware.rb
isomorfeus-transport-2.0.0.rc4 lib/isomorfeus/transport/rack_middleware.rb
isomorfeus-transport-2.0.0.rc3 lib/isomorfeus/transport/rack_middleware.rb
isomorfeus-transport-2.0.0.rc2 lib/isomorfeus/transport/rack_middleware.rb
isomorfeus-transport-2.0.0.rc1 lib/isomorfeus/transport/rack_middleware.rb