From 0cd7e6c1e02f9fab6939963a42b95fe2245fac19 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 9 Sep 2005 08:59:07 +0000 Subject: Added -c/--charset option to WEBrick controller, so you can specify a default charset (which without changes is UTF-8) #2084 [wejn@box.cz] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2173 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- railties/CHANGELOG | 2 ++ railties/bin/server | 7 ++++++- railties/lib/webrick_server.rb | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/railties/CHANGELOG b/railties/CHANGELOG index d1760e0b59..6ceb17c842 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added -c/--charset option to WEBrick controller, so you can specify a default charset (which without changes is UTF-8) #2084 [wejn@box.cz] + * Make the default stats task extendable by modifying the STATS_DIRECTORIES constant * Allow the selected environment to define RAILS_DEFAULT_LOGGER, and have Rails::Initializer use it if it exists. diff --git a/railties/bin/server b/railties/bin/server index 487d1fd954..3a8cc68258 100644 --- a/railties/bin/server +++ b/railties/bin/server @@ -8,7 +8,8 @@ OPTIONS = { :ip => "0.0.0.0", :environment => "development", :server_root => File.expand_path(File.dirname(__FILE__) + "/../public/"), - :server_type => WEBrick::SimpleServer + :server_type => WEBrick::SimpleServer, + :charset => "UTF-8" } ARGV.options do |opts| @@ -30,6 +31,10 @@ ARGV.options do |opts| "Make Rails run as a Daemon (only works if fork is available -- meaning on *nix)." ) { OPTIONS[:server_type] = WEBrick::Daemon } + opts.on("-c", "--charset=charset", String, + "Set default charset for output.", + "Default: UTF-8") { |OPTIONS[:charset]| } + opts.separator "" opts.on("-h", "--help", diff --git a/railties/lib/webrick_server.rb b/railties/lib/webrick_server.rb index ba0541e7c6..4a73872ba3 100644 --- a/railties/lib/webrick_server.rb +++ b/railties/lib/webrick_server.rb @@ -109,6 +109,8 @@ class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet ) header, body = extract_header_and_body(data) + + set_charset(header) assign_status(res, header) res.cookies.concat(header.delete('set-cookie')) header.each { |key, val| res[key] = val.join(", ") } @@ -138,6 +140,14 @@ class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet return header, body end + + def set_charset(header) + ct = header["content-type"] + if ct.any? { |x| x =~ /^text\// } && ! ct.any? { |x| x =~ /charset=/ } + ch = @server_options[:charset] || "UTF-8" + ct.find { |x| x =~ /^text\// } << ("; charset=" + ch) + end + end def assign_status(res, header) if /^(\d+)/ =~ header['status'][0] -- cgit v1.2.3