Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb
index 9718363..eea1729 100644
--- a/lib/sinatra/base.rb
+++ b/lib/sinatra/base.rb
@@ -1360,12 +1360,18 @@ module Sinatra
# Fixes encoding issues by
# * defaulting to UTF-8
- # * casting params to Encoding.default_external
+ # * casting params to default_encoding
# The latter might not be necessary if Rack handles it one day.
# Keep an eye on Rack's LH #100.
def force_encoding(*args) settings.force_encoding(*args) end
if defined? Encoding
+ if Encoding.default_external == Encoding::ASCII
+ set :default_encoding, "utf-8"
+ else
+ set :default_encoding, Encoding.default_external.to_s.downcase
+ end
def self.force_encoding(data, encoding = default_encoding)
return if data == settings || data.is_a?(Tempfile)
if data.respond_to? :force_encoding
@@ -1390,7 +1396,6 @@ module Sinatra
set :sessions, false
set :logging, false
set :method_override, false
- set :default_encoding, "utf-8"
set :add_charset, [/^text\//, 'application/javascript', 'application/xml', 'application/xhtml+xml']
# explicitly generating a session secret eagerly to play nice with preforking
diff --git a/lib/tilt/template.rb b/lib/tilt/template.rb
index 4852ee0..ba63706 100644
--- a/lib/tilt/template.rb
+++ b/lib/tilt/template.rb
@@ -60,11 +60,19 @@ module Tilt
@compiled_method = {}
# used on 1.9 to set the encoding if it is not set elsewhere (like a magic comment)
- # currently only used if template compiles to ruby
@default_encoding = @options.delete :default_encoding
+ @default_encoding ||= Encoding.default_external if defined? Encoding.default_external
+ # decide how to load template data
+ @reader = block || begin
+ if File.respond_to? :binread
+ lambda { |t|, :encoding => @default_encoding, :mode => 'rb') }
+ else
+ lambda { |t| }
+ end
+ end
- # load template data and prepare (uses binread to avoid encoding issues)
- @reader = block || lambda { |t| File.respond_to?(:binread) ? File.binread(@file) : }
+ # actually load template data and prepare
@data =

Looks great!


The Sinatra patch needs adjustments (we also need to set default_encoding on 1.8, since we use it for HTTP headers).


Why you need to convert from ASCII to UTF8 ? If one have their source in ascii and want to keep them in ascii ?

if Encoding.default_external == Encoding::ASCII
  set :default_encoding, "utf-8"
  set :default_encoding, Encoding.default_external.to_s.downcase

See discussion (default value, error on deployment), it might even be possible that the server starts and crashes as soon as a user submits UTF-8 characters in a from.


Okey, thanks for the explanation! Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.