<commit_before>unless defined? RAILS_ROOT
  raise "Rails' environment has to be loaded before using Rack::Adapter::Rails"
end

require "dispatcher"

module Rack
  module Adapter
    # TODO: Extract this
    class Rails
      def initialize
        @file = Rack::File.new(::File.join(RAILS_ROOT, "public"))
        @dispatcher = RailsDispatcher.new
      end
      
      def call(env)
        file_response = @file.call(env)
        status = file_response.first
        
        if status == 200
          file_response
        else
          @dispatcher.call(env)
        end
      end
    end
    
    class RailsDispatcher
      def call(env)
        response = dispatch(CGIStub.new(env))
        headers  = response.headers
        status   = headers.delete("Status")
        
        [ status, headers, response.body ]
      end
      
      protected
      
      def dispatch(cgi)
        session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS

        request  = ActionController::CgiRequest.new(cgi, session_options)
        response = ActionController::CgiResponse.new(cgi)

        Dispatcher.send(:prepare_application)

        controller = ActionController::Routing::Routes.recognize(request)
        controller.process(request, response)

        return response
      end
      
      class RailsDispatcher::CGIStub
        def initialize(env) @env = env end
        def env_table() @env end
        def params() {} end
        def cookies() {} end
        def query_string() @env["QUERY_STRING"] end

        def key?(key)
          p "Rack::CGI#key? called with #{key.inspect}"
          false
        end
      end
    end
  end
end<commit_msg>Extended CGIStub to handle Rails' session cookie<commit_after>unless defined? RAILS_ROOT
  raise "Rails' environment has to be loaded before using Rack::Adapter::Rails"
end

require "dispatcher"

module Rack
  module Adapter
    # TODO: Extract this
    class Rails
      def initialize
        @file = Rack::File.new(::File.join(RAILS_ROOT, "public"))
        @dispatcher = RailsDispatcher.new
      end
      
      def call(env)
        file_response = @file.call(env)
        status = file_response.first
        
        if status == 200
          file_response
        else
          @dispatcher.call(env)
        end
      end
    end
    
    class RailsDispatcher
      def call(env)
        response = dispatch(CGIStub.new(env))
        headers  = response.headers
        status   = headers.delete("Status")
        
        [ status, headers, response.body ]
      end
      
      protected
      
      def dispatch(cgi)
        session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS

        request  = ActionController::CgiRequest.new(cgi, session_options)
        response = ActionController::CgiResponse.new(cgi)

        Dispatcher.send(:prepare_application)

        controller = ActionController::Routing::Routes.recognize(request)
        controller.process(request, response)

        return response
      end
      
      class RailsDispatcher::CGIStub
        def initialize(env) @request = Request.new(env) end
        def env_table() @request.env end
        def params() @request.params end
        def cookies() @request.cookies end
        def query_string() @request.env["QUERY_STRING"] end
          
        def [](key)
          # FIXME: This is probably just wrong
          @request.env[key] || @request.cookies[key]
        end

        def key?(key)
          (self[key]) ? true : false
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08480763830384723, 'fraction_numerical': 0.0016849199663016006, 'mean_word_length': 2.1136363636363638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '740869', 'n_tokens_mistral': 1018, 'n_tokens_neox': 981, 'n_words': 243}