class RackEncodingScrubber

Constants

REGEX_MB
REGEX_UTF

Public Class Methods

new(app) click to toggle source
# File lib/rack_encoding_scrubber.rb, line 6
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack_encoding_scrubber.rb, line 10
def call(env)
  encode env
  @app.call(env)
end
encode(env) click to toggle source
# File lib/rack_encoding_scrubber.rb, line 15
def encode(env)
  request_method = env['REQUEST_METHOD']
  if request_method == 'GET'
    %w[QUERY_STRING REQUEST_PATH PATH_INFO QUERY_STRING REQUEST_URI ORIGINAL_FULLPATH].each do |header|
      if h = env[header]
        if h["%u00"]
          h.gsub!(REGEX_UTF, "")
        end
        if h[REGEX_MB] # check for byte
          tmp = CGI.unescape(h).force_encoding('utf-8')
          if !tmp.valid_encoding?
            env[header] = CGI.escape(tmp.scrub(''))
            if %w[REQUEST_PATH PATH_INFO REQUEST_URI].include? header
              env[header].gsub! '%2F', '/'
            end
          end
        end
      end
    end
  end
end