“警告不能确定响应体的内容长度”。“是什么意思,我怎么摆脱它?”

时间:2022-02-21 21:14:17

Since upgrading to Rails 3.1 I'm seeing this warning message in my development log:

升级到Rails 3.1后,我在开发日志中看到了这个警告信息:

WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

警告不能确定响应体的内容长度。设置响应的内容长度或设置响应#chunked = true。

What does this mean and how can I remove it? Is it a problem?

这是什么意思,我该如何删除它?这是一个问题吗?

9 个解决方案

#1


227  

Asked the same question to one of Rails-Core's members:

同样的问题也问了railscore的成员:

https://twitter.com/#!/luislavena/status/108998968859566080

https://twitter.com/ # ! / luislavena /地位/ 108998968859566080

And the answer:

答案:

https://twitter.com/#!/tenderlove/status/108999110136303617

https://twitter.com/ # ! / tenderlove /地位/ 108999110136303617

ya, it's fine. Need to clean it up, but nothing is being hurt.

是的,很好。需要清理干净,但什么都没有伤害。

#2


78  

The following patch solved the problem in my case; no more warnings for me.

下面的补丁解决了我的问题;不要再警告我了。

204_304_keep_alive.patch

204年_304_keep_alive.patch

Just edit the file httpresponse.rb at line 205 as shown at the link above; in fact the link shows a correction made to a future release of Ruby.

只需编辑文件httpresponse。在第205行的rb,如上面的链接所示;事实上,这个链接显示了对Ruby未来版本的修正。

I'm using rails 3.2.0 on ruby 1.9.3-p0 installed through RVM as a single user. So the location in my case is:

我使用的是ruby 1.9.3-p0上的rails 3.2.0,通过RVM作为单个用户安装。所以我的情况是:

~/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpresponse.rb

The location of the file to be altered differs depending on the type of installation, RVM or not, or even multi-user or single user, so I'm just giving the last part of it:

要更改的文件的位置取决于安装的类型、RVM或者不是,甚至是多用户或单个用户,所以我只是给出了最后一部分:

.../ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpresponse.rb

I hope this can be helpful to someone.

我希望这对某人有帮助。

EDIT: This is the link to the commit that altered the line in question in the trunk branch of ruby project.

编辑:这是在ruby项目的主干分支中修改问题行的链接。

#3


55  

Just explicitly adding the Gem to the Gemfile got rid of the warning messages for me:

只需在Gemfile中显式添加Gem,就可以删除对我的警告信息:

group :development do
  gem 'webrick', '~> 1.3.1'
end

#4


54  

You can also use Thin instead of the default Webrick. Add this to Gemfile gem 'thin'

你也可以使用薄而不是默认的Webrick。将此添加到Gemfile gem 'thin'

then rails s thin will use thin, and the warning will disappear.

然后,rails的thin将使用thin,警告将会消失。

#5


15  

If you're using .rvm, do this to fix it...

如果您正在使用.rvm,请执行此操作…

As mentioned by João Soares, all credits to him, this is what you can do if you wan't to get rid of this warning on development.

如Joao Soares所提到的,这是他的全部功劳,如果你不能摆脱对发展的警告,这就是你能做的。

  1. Use your favorite editor to open this file:

    使用您喜欢的编辑器打开这个文件:

    ~/.rvm/rubies/<ruby-version>/lib/ruby/1.9.1/webrick/httpresponse.rb
    
  2. Go to the line that contains this(for me it was really line 206):

    走到包含这条线的那条线(对我来说,这条线真的是206行):

    if chunked? || @header['content-length']
    
  3. Change it, taken from this patch, to this:

    改变它,从这个补丁,到这个:

    if chunked? || @header['content-length'] || @status == 304 || @status == 204
    
  4. Save the file and eventually restart your rails server

    保存文件并最终重新启动rails服务器。

#6


12  

This problem has been fixed in Ruby's trunk branch with this commit to webrick.

这个问题已经在Ruby的主干分支中得到了解决,并将其提交给webrick。

You can edit this particular webrick file similarly in your setup. The approximate location can be found by:

您可以在您的设置中类似地编辑这个webrick文件。大致的位置可以找到:

gem which webrick

To actually edit the file:

要实际编辑文件:

nano \`ruby -e"print %x{gem which webrick}.chomp %Q{.rb\n}"\`/httpresponse.rb

(Or instead of nano, use your favorite editor.)

(或者用你最喜欢的编辑器代替nano。)

#7


5  

JRuby version: If you're using .rvm, do this to fix it...

JRuby版本:如果您正在使用.rvm,那么就来修复它……

As mentioned by João Soares and Kjellski, this is what you can do if you want to get rid of this warning on development and you are using JRuby.

正如Joao Soares和Kjellski所提到的,如果您想要摆脱开发的警告,并且使用JRuby,您就可以这样做。

  1. Use your favorite editor to open this file:

    使用您喜欢的编辑器打开这个文件:

    ~/.rvm/rubies/jruby-<version>/lib/ruby/<1.8 or 1.9>/webrick/httpresponse.rb
    
  2. Go to the line that contains this (for me it was line 205):

    到包含这个的行(对我来说是第205行):

    if chunked? || @header['content-length']
    
  3. Change it, taken from this patch, to this:

    改变它,从这个补丁,到这个:

    if chunked? || @header['content-length'] || @status == 304 || @status == 204
    
  4. Save the file and eventually restart your rails server.

    保存文件并最终重新启动rails服务器。

#8


3  

Another workaround that removes the offending line from webrick. It's just not that useful:

另一种解决方法是从webrick移除违规行。只是没有那么有用:

cd `which ruby`/../../lib/ruby/1.9.1/webrick/ && sed -i '.bak' -e'/logger.warn/d' httpresponse.rb

(you may need to sudo)

(你可能需要sudo)

#9


2  

Add

添加

config.middleware.use Rack::ContentLength

to your application.rb file, and the warning will disappear even with webrick. This will also set Content-Length properly in production when rendering a json or text response.

到您的应用程序。rb文件,即使是webrick,警告也会消失。这还将在呈现json或文本响应时适当地设置内容长度。

#1


227  

Asked the same question to one of Rails-Core's members:

同样的问题也问了railscore的成员:

https://twitter.com/#!/luislavena/status/108998968859566080

https://twitter.com/ # ! / luislavena /地位/ 108998968859566080

And the answer:

答案:

https://twitter.com/#!/tenderlove/status/108999110136303617

https://twitter.com/ # ! / tenderlove /地位/ 108999110136303617

ya, it's fine. Need to clean it up, but nothing is being hurt.

是的,很好。需要清理干净,但什么都没有伤害。

#2


78  

The following patch solved the problem in my case; no more warnings for me.

下面的补丁解决了我的问题;不要再警告我了。

204_304_keep_alive.patch

204年_304_keep_alive.patch

Just edit the file httpresponse.rb at line 205 as shown at the link above; in fact the link shows a correction made to a future release of Ruby.

只需编辑文件httpresponse。在第205行的rb,如上面的链接所示;事实上,这个链接显示了对Ruby未来版本的修正。

I'm using rails 3.2.0 on ruby 1.9.3-p0 installed through RVM as a single user. So the location in my case is:

我使用的是ruby 1.9.3-p0上的rails 3.2.0,通过RVM作为单个用户安装。所以我的情况是:

~/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpresponse.rb

The location of the file to be altered differs depending on the type of installation, RVM or not, or even multi-user or single user, so I'm just giving the last part of it:

要更改的文件的位置取决于安装的类型、RVM或者不是,甚至是多用户或单个用户,所以我只是给出了最后一部分:

.../ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpresponse.rb

I hope this can be helpful to someone.

我希望这对某人有帮助。

EDIT: This is the link to the commit that altered the line in question in the trunk branch of ruby project.

编辑:这是在ruby项目的主干分支中修改问题行的链接。

#3


55  

Just explicitly adding the Gem to the Gemfile got rid of the warning messages for me:

只需在Gemfile中显式添加Gem,就可以删除对我的警告信息:

group :development do
  gem 'webrick', '~> 1.3.1'
end

#4


54  

You can also use Thin instead of the default Webrick. Add this to Gemfile gem 'thin'

你也可以使用薄而不是默认的Webrick。将此添加到Gemfile gem 'thin'

then rails s thin will use thin, and the warning will disappear.

然后,rails的thin将使用thin,警告将会消失。

#5


15  

If you're using .rvm, do this to fix it...

如果您正在使用.rvm,请执行此操作…

As mentioned by João Soares, all credits to him, this is what you can do if you wan't to get rid of this warning on development.

如Joao Soares所提到的,这是他的全部功劳,如果你不能摆脱对发展的警告,这就是你能做的。

  1. Use your favorite editor to open this file:

    使用您喜欢的编辑器打开这个文件:

    ~/.rvm/rubies/<ruby-version>/lib/ruby/1.9.1/webrick/httpresponse.rb
    
  2. Go to the line that contains this(for me it was really line 206):

    走到包含这条线的那条线(对我来说,这条线真的是206行):

    if chunked? || @header['content-length']
    
  3. Change it, taken from this patch, to this:

    改变它,从这个补丁,到这个:

    if chunked? || @header['content-length'] || @status == 304 || @status == 204
    
  4. Save the file and eventually restart your rails server

    保存文件并最终重新启动rails服务器。

#6


12  

This problem has been fixed in Ruby's trunk branch with this commit to webrick.

这个问题已经在Ruby的主干分支中得到了解决,并将其提交给webrick。

You can edit this particular webrick file similarly in your setup. The approximate location can be found by:

您可以在您的设置中类似地编辑这个webrick文件。大致的位置可以找到:

gem which webrick

To actually edit the file:

要实际编辑文件:

nano \`ruby -e"print %x{gem which webrick}.chomp %Q{.rb\n}"\`/httpresponse.rb

(Or instead of nano, use your favorite editor.)

(或者用你最喜欢的编辑器代替nano。)

#7


5  

JRuby version: If you're using .rvm, do this to fix it...

JRuby版本:如果您正在使用.rvm,那么就来修复它……

As mentioned by João Soares and Kjellski, this is what you can do if you want to get rid of this warning on development and you are using JRuby.

正如Joao Soares和Kjellski所提到的,如果您想要摆脱开发的警告,并且使用JRuby,您就可以这样做。

  1. Use your favorite editor to open this file:

    使用您喜欢的编辑器打开这个文件:

    ~/.rvm/rubies/jruby-<version>/lib/ruby/<1.8 or 1.9>/webrick/httpresponse.rb
    
  2. Go to the line that contains this (for me it was line 205):

    到包含这个的行(对我来说是第205行):

    if chunked? || @header['content-length']
    
  3. Change it, taken from this patch, to this:

    改变它,从这个补丁,到这个:

    if chunked? || @header['content-length'] || @status == 304 || @status == 204
    
  4. Save the file and eventually restart your rails server.

    保存文件并最终重新启动rails服务器。

#8


3  

Another workaround that removes the offending line from webrick. It's just not that useful:

另一种解决方法是从webrick移除违规行。只是没有那么有用:

cd `which ruby`/../../lib/ruby/1.9.1/webrick/ && sed -i '.bak' -e'/logger.warn/d' httpresponse.rb

(you may need to sudo)

(你可能需要sudo)

#9


2  

Add

添加

config.middleware.use Rack::ContentLength

to your application.rb file, and the warning will disappear even with webrick. This will also set Content-Length properly in production when rendering a json or text response.

到您的应用程序。rb文件,即使是webrick,警告也会消失。这还将在呈现json或文本响应时适当地设置内容长度。