Skip to content

Instantly share code, notes, and snippets.

@zhangyuan
Forked from phoet/char_converter.rb
Last active December 24, 2015 05:19
Show Gist options
  • Save zhangyuan/6750038 to your computer and use it in GitHub Desktop.
Save zhangyuan/6750038 to your computer and use it in GitHub Desktop.
# config/initializers/char_converter.rb
require 'uri'
module Support
class CharConverter
def initialize(app)
@app = app
end
def call(env)
@app.call(self.class.sanitize_env(env))
end
def self.sanitize_env(env)
["HTTP_REFERER", "PATH_INFO", "QUERY_STRING", "REQUEST_PATH", "REQUEST_URI"].each do |key|
next unless value = env[key]
fixed = sanitize_string(URI.decode(value))
env[key] = URI.encode(fixed) if fixed
end
["HTTP_COOKIE"].each do |key|
next unless value = env[key]
fixed = sanitize_string(value)
env[key] = fixed if fixed
end
env
end
def self.sanitize_string(string)
return if !string.is_a?(String) || string == ''
# Try it as UTF-8 directly
cleaned = string.dup.force_encoding(Encoding::UTF_8)
# Some of it might be old Windows code page
# GBK for IE
cleaned.encode(Encoding::UTF_8, Encoding::GBK) unless cleaned.valid_encoding?
rescue EncodingError
# Force it to UTF-8, throwing out invalid bits
cleaned.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment