Skip to content

Instantly share code, notes, and snippets.

@rkh
Created May 10, 2011 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkh/964024 to your computer and use it in GitHub Desktop.
Save rkh/964024 to your computer and use it in GitHub Desktop.
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| File.read(@file, :encoding => @default_encoding, :mode => 'rb') }
+ else
+ lambda { |t| File.read(@file) }
+ end
+ end
- # load template data and prepare (uses binread to avoid encoding issues)
- @reader = block || lambda { |t| File.respond_to?(:binread) ? File.binread(@file) : File.read(@file) }
+ # actually load template data and prepare
@data = @reader.call(self)
prepare
end
@DAddYE
Copy link

DAddYE commented May 10, 2011

Looks great!

@rkh
Copy link
Author

rkh commented May 10, 2011

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

@DAddYE
Copy link

DAddYE commented May 10, 2011

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"
else
  set :default_encoding, Encoding.default_external.to_s.downcase
end

@rkh
Copy link
Author

rkh commented May 10, 2011

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.

@DAddYE
Copy link

DAddYE commented May 10, 2011

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