<commit_before>require 'digest/sha1'

module Rack
  class Cookieless
    attr_accessor :session_id, :cache_id
    def initialize(app, options={})
      @app, @options = app, options
    end

    def cache_store
      @options[:cache_store] || Rails.cache
    end

    def session_key
      (@options[:session_id] || :session_id).to_s
    end

    def call(env)
      if support_cookie?(env)
        @app.call(env)
      else
        set_session_id(Rack::Utils.parse_query(env["QUERY_STRING"], "&")[session_key].to_s, env)
        env["rack.session"].update(cache_store.fetch(@cache_id) { env["rack.session"] }) if @cache_id

        status, header, response = @app.call(env)

        set_session_id(env["rack.session"]["session_id"], env) if @session_id.blank?
        cache_store.write(@cache_id, env["rack.session"].to_hash)

        response.body = process_body(response.body) if response.respond_to?(:body)
        [status, header, response]
      end
    end

    def set_session_id(session_id, env)
      @session_id = session_id
      @cache_id = Digest::SHA1.hexdigest(session_id + env["HTTP_USER_AGENT"] + env["REMOTE_ADDR"]) if @session_id.present?
    end

    def process_body(body)
      body_doc = Nokogiri::HTML(body)
      #TODO: change hardcode "?session_id"
      body_doc.css("a").map { |a| a["href"] += "?#{session_key}=#{@session_id}" if a["href"] }
      body_doc.css("form").map { |form| form["action"] += "?#{session_key}=#{@session_id}" if form["action"] }
      body_doc.to_html
    end<commit_msg>fix session_id haven't been changed for different users<commit_after>require 'digest/sha1'

module Rack
  class Cookieless
    def initialize(app, options={})
      @app, @options = app, options
    end

    def cache_store
      @options[:cache_store] || Rails.cache
    end

    def session_key
      (@options[:session_id] || :session_id).to_s
    end

    def call(env)
      if support_cookie?(env)
        @app.call(env)
      else
        session_id, cache_id = set_session_id(Rack::Utils.parse_query(env["QUERY_STRING"], "&")[session_key].to_s, env)
        env["rack.session"].update(cache_store.fetch(cache_id) { env["rack.session"] }) if cache_id

        status, header, response = @app.call(env)

        session_id, cache_id = set_session_id(env["rack.session"]["session_id"], env) if session_id.blank?
        cache_store.write(cache_id, env["rack.session"].to_hash)

        response.body = process_body(response.body, session_id) if response.respond_to?(:body)
        [status, header, response]
      end
    end

    def set_session_id(session_id, env)
      [session_id, (session_id.present? ? Digest::SHA1.hexdigest(session_id + env["HTTP_USER_AGENT"] + env["REMOTE_ADDR"]) : nil)]
    end

    def process_body(body, session_id)
      body_doc = Nokogiri::HTML(body)
      #TODO: change hardcode "?session_id"
      body_doc.css("a").map { |a| a["href"] += "?#{session_key}=#{session_id}" if a["href"] }
      body_doc.css("form").map { |form| form["action"] += "?#{session_key}=#{session_id}" if form["action"] }
      body_doc.to_html
    end
{'fraction_non_alphanumeric': 0.15016181229773462, 'fraction_numerical': 0.0012944983818770227, 'mean_word_length': 3.5256222547584186, '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': '719133', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1060, 'n_words': 193}