为什么此rackup 文件适用于Thin,但不适用于WEBrick 或Unicorn?
我在本地运行静态网站(用于测试)时遇到一个奇怪的问题。 导航到 root 时,WEBrick 和 Unicorn 都会导致 Rack 中断言失败。然而薄的作品 完美。
我的机架文件“config.ru”:
# This is the root of our app
@root = File.expand_path(File.dirname(__FILE__)) + '/site'
default_charset = "; charset=UTF-8"
run Proc.new { |env|
# Extract the requested path from the request
path = Rack::Utils.unescape(env['PATH_INFO'])
index_file = @root + "#{path}/index.html"
if File.exists?(index_file)
# Return the index
[200, {'Content-Type' => 'text/html' + default_charset}, File.read(index_file)]
else
# Pass the request to the directory app
response = Rack::Directory.new(@root).call(env)
if response[1]['Content-Type']
response[1]['Content-Type'] += default_charset
end
response
end
}
宝石列表如下:
$ bundle exec gem list
*** LOCAL GEMS ***
bundler (1.0.18)
coderay (0.9.8)
daemons (1.1.4)
erector (0.8.3)
eventmachine (0.12.10)
kgio (2.6.0)
kramdown (0.13.3)
polyglot (0.3.2)
rack (1.3.2)
raindrops (0.7.0)
rake (0.9.2)
thin (1.2.11)
treetop (1.4.10)
unicorn (4.1.1)
这些是我得到的错误:
$ bundle exec rackup
[2011-09-12 21:19:50] INFO WEBrick 1.3.1
[2011-09-12 21:19:50] INFO ruby 1.9.2 (2010-08-18) [x86_64-linux]
[2011-09-12 21:19:50] INFO WEBrick::HTTPServer#start: pid=2556 port=9292
127.0.0.1 - - [12/Sep/2011 21:20:05] "GET / HTTP/1.1" 200 - 0.0027
[2011-09-12 21:20:05] ERROR Rack::Lint::LintError: Response body must respond to each
/home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/lint.rb:19:in `assert'
/home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/lint.rb:513:in `each'
/home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/body_proxy.rb:26:in `method_missing'
/home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/chunked.rb:23:in `each'
/home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/handler/webrick.rb:71:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/usr/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
$ bundle exec unicorn
I, [2011-09-12T21:20:33.219694 #2567] INFO -- : listening on addr=0.0.0.0:8080 fd=3
I, [2011-09-12T21:20:33.219883 #2567] INFO -- : worker=0 spawning...
I, [2011-09-12T21:20:33.220771 #2567] INFO -- : master process ready
I, [2011-09-12T21:20:33.221535 #2570] INFO -- : worker=0 spawned pid=2570
I, [2011-09-12T21:20:33.221787 #2570] INFO -- : Refreshing Gem list
I, [2011-09-12T21:20:33.253184 #2570] INFO -- : worker=0 ready
127.0.0.1 - - [12/Sep/2011 21:20:41] "GET / HTTP/1.1" 200 - 0.0030
E, [2011-09-12T21:20:41.087884 #2570] ERROR -- : app error: Response body must respond to each (Rack::Lint::LintError)
E, [2011-09-12T21:20:41.087935 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/lint.rb:19:in `assert'
E, [2011-09-12T21:20:41.087968 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/lint.rb:513:in `each'
E, [2011-09-12T21:20:41.087991 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/body_proxy.rb:26:in `method_missing'
E, [2011-09-12T21:20:41.088013 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/chunked.rb:23:in `each'
E, [2011-09-12T21:20:41.088035 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_response.rb:41:in `http_response_write'
E, [2011-09-12T21:20:41.088057 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_server.rb:536:in `process_client'
E, [2011-09-12T21:20:41.088078 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_server.rb:600:in `worker_loop'
E, [2011-09-12T21:20:41.088100 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_server.rb:485:in `spawn_missing_workers'
E, [2011-09-12T21:20:41.088124 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_server.rb:135:in `start'
E, [2011-09-12T21:20:41.088147 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/unicorn-4.1.1/bin/unicorn:121:in `<top (required)>'
E, [2011-09-12T21:20:41.088168 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/bin/unicorn:19:in `load'
E, [2011-09-12T21:20:41.088190 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/bin/unicorn:19:in `<main>'
并且瘦工作正常:
$bundle exec thin start
>> Using rack adapter
>> Thin web server (v1.2.11 codename Bat-Shit Crazy)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
^C>> Stopping ...
为什么会发生这种情况,当所有三台服务器都应该遵守 机架规格?
I am having a strange issue running my static website locally (for testing).
Both WEBrick and Unicorn are causing an assertion failure in Rack when navigating to root. However Thin works
perfectly.
My rackup file 'config.ru':
# This is the root of our app
@root = File.expand_path(File.dirname(__FILE__)) + '/site'
default_charset = "; charset=UTF-8"
run Proc.new { |env|
# Extract the requested path from the request
path = Rack::Utils.unescape(env['PATH_INFO'])
index_file = @root + "#{path}/index.html"
if File.exists?(index_file)
# Return the index
[200, {'Content-Type' => 'text/html' + default_charset}, File.read(index_file)]
else
# Pass the request to the directory app
response = Rack::Directory.new(@root).call(env)
if response[1]['Content-Type']
response[1]['Content-Type'] += default_charset
end
response
end
}
Gem list is as follows:
$ bundle exec gem list
*** LOCAL GEMS ***
bundler (1.0.18)
coderay (0.9.8)
daemons (1.1.4)
erector (0.8.3)
eventmachine (0.12.10)
kgio (2.6.0)
kramdown (0.13.3)
polyglot (0.3.2)
rack (1.3.2)
raindrops (0.7.0)
rake (0.9.2)
thin (1.2.11)
treetop (1.4.10)
unicorn (4.1.1)
And these are the errors I get:
$ bundle exec rackup
[2011-09-12 21:19:50] INFO WEBrick 1.3.1
[2011-09-12 21:19:50] INFO ruby 1.9.2 (2010-08-18) [x86_64-linux]
[2011-09-12 21:19:50] INFO WEBrick::HTTPServer#start: pid=2556 port=9292
127.0.0.1 - - [12/Sep/2011 21:20:05] "GET / HTTP/1.1" 200 - 0.0027
[2011-09-12 21:20:05] ERROR Rack::Lint::LintError: Response body must respond to each
/home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/lint.rb:19:in `assert'
/home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/lint.rb:513:in `each'
/home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/body_proxy.rb:26:in `method_missing'
/home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/chunked.rb:23:in `each'
/home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/handler/webrick.rb:71:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/usr/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
$ bundle exec unicorn
I, [2011-09-12T21:20:33.219694 #2567] INFO -- : listening on addr=0.0.0.0:8080 fd=3
I, [2011-09-12T21:20:33.219883 #2567] INFO -- : worker=0 spawning...
I, [2011-09-12T21:20:33.220771 #2567] INFO -- : master process ready
I, [2011-09-12T21:20:33.221535 #2570] INFO -- : worker=0 spawned pid=2570
I, [2011-09-12T21:20:33.221787 #2570] INFO -- : Refreshing Gem list
I, [2011-09-12T21:20:33.253184 #2570] INFO -- : worker=0 ready
127.0.0.1 - - [12/Sep/2011 21:20:41] "GET / HTTP/1.1" 200 - 0.0030
E, [2011-09-12T21:20:41.087884 #2570] ERROR -- : app error: Response body must respond to each (Rack::Lint::LintError)
E, [2011-09-12T21:20:41.087935 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/lint.rb:19:in `assert'
E, [2011-09-12T21:20:41.087968 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/lint.rb:513:in `each'
E, [2011-09-12T21:20:41.087991 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/body_proxy.rb:26:in `method_missing'
E, [2011-09-12T21:20:41.088013 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/rack-1.3.2/lib/rack/chunked.rb:23:in `each'
E, [2011-09-12T21:20:41.088035 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_response.rb:41:in `http_response_write'
E, [2011-09-12T21:20:41.088057 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_server.rb:536:in `process_client'
E, [2011-09-12T21:20:41.088078 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_server.rb:600:in `worker_loop'
E, [2011-09-12T21:20:41.088100 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_server.rb:485:in `spawn_missing_workers'
E, [2011-09-12T21:20:41.088124 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_server.rb:135:in `start'
E, [2011-09-12T21:20:41.088147 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/gems/unicorn-4.1.1/bin/unicorn:121:in `<top (required)>'
E, [2011-09-12T21:20:41.088168 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/bin/unicorn:19:in `load'
E, [2011-09-12T21:20:41.088190 #2570] ERROR -- : /home/scott/development/test/bundle/ruby/1.9.1/bin/unicorn:19:in `<main>'
And thin working fine:
$bundle exec thin start
>> Using rack adapter
>> Thin web server (v1.2.11 codename Bat-Shit Crazy)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
^C>> Stopping ...
Why is this happening, when all three servers are supposed to adhere to the
Rack specification?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这三个都运行在 ruby 1.9 上吗?
如果您查看机架规格(此处找到:http://rack.rubyforge.org/doc/ SPEC.html)在主体部分下说:
主体必须响应每个并且只能产生字符串值。 Body 本身不应该是 String 的实例,因为这在 Ruby 1.9 中会被破坏。
因此,根据规范,我猜测要么 Thin 在检查它对每个的响应方面有点懈怠,要么红宝石版本可能存在差异?
Are all three running on ruby 1.9?
If you check out the rack spec (found here: http://rack.rubyforge.org/doc/SPEC.html) under the body section it says:
The Body must respond to each and must only yield String values. The Body itself should not be an instance of String, as this will break in Ruby 1.9.
So according to the spec i'd guess either thin is a little slacker in checking it responds to each or perhaps theres a difference in the ruby versions?