Skip to content
This repository
Browse code

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
  • Loading branch information...
commit 0cd7e6c1e02f9fab6939963a42b95fe2245fac19 1 parent 0faca07
David Heinemeier Hansson authored September 09, 2005
2  railties/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Added -c/--charset option to WEBrick controller, so you can specify a default charset (which without changes is UTF-8) #2084 [wejn@box.cz]
  4
+
3 5
 * Make the default stats task extendable by modifying the STATS_DIRECTORIES constant
4 6
 
5 7
 * Allow the selected environment to define RAILS_DEFAULT_LOGGER, and have Rails::Initializer use it if it exists.
7  railties/bin/server
@@ -8,7 +8,8 @@ OPTIONS = {
8 8
   :ip          => "0.0.0.0",
9 9
   :environment => "development",
10 10
   :server_root => File.expand_path(File.dirname(__FILE__) + "/../public/"),
11  
-  :server_type => WEBrick::SimpleServer
  11
+  :server_type => WEBrick::SimpleServer,
  12
+  :charset     => "UTF-8"
12 13
 }
13 14
 
14 15
 ARGV.options do |opts|
@@ -30,6 +31,10 @@ ARGV.options do |opts|
30 31
           "Make Rails run as a Daemon (only works if fork is available -- meaning on *nix)."
31 32
           ) { OPTIONS[:server_type] = WEBrick::Daemon }
32 33
 
  34
+  opts.on("-c", "--charset=charset", String,
  35
+          "Set default charset for output.",
  36
+          "Default: UTF-8") { |OPTIONS[:charset]| }
  37
+
33 38
   opts.separator ""
34 39
 
35 40
   opts.on("-h", "--help",
10  railties/lib/webrick_server.rb
@@ -109,6 +109,8 @@ def handle_dispatch(req, res, origin = nil)
109 109
     )
110 110
 
111 111
     header, body = extract_header_and_body(data)
  112
+
  113
+    set_charset(header)
112 114
     assign_status(res, header)
113 115
     res.cookies.concat(header.delete('set-cookie'))
114 116
     header.each { |key, val| res[key] = val.join(", ") }
@@ -138,6 +140,14 @@ def extract_header_and_body(data)
138 140
       
139 141
       return header, body
140 142
     end
  143
+    
  144
+    def set_charset(header)
  145
+      ct = header["content-type"]
  146
+      if ct.any? { |x| x =~ /^text\// } && ! ct.any? { |x| x =~ /charset=/ }
  147
+        ch = @server_options[:charset] || "UTF-8"
  148
+        ct.find { |x| x =~ /^text\// } << ("; charset=" + ch)
  149
+      end
  150
+    end
141 151
 
142 152
     def assign_status(res, header)
143 153
       if /^(\d+)/ =~ header['status'][0]

0 notes on commit 0cd7e6c

Please sign in to comment.
Something went wrong with that request. Please try again.