“警告无法确定响应正文的内容长度。”是什么意思?意思是我该如何摆脱它?

发布于 2024-11-29 21:43:27 字数 172 浏览 1 评论 0原文

自从升级到 Rails 3.1 以来,我在开发日志中看到以下警告消息:

警告无法确定响应正文的内容长度。设置响应的内容长度或设置Response#chunked = true

这是什么意思以及如何删除它?有问题吗?

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

人心善变 2024-12-06 21:43:27

向 Rails-Core 的一位成员提出了同样的问题:

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

答案:

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

是的,没关系。需要清理它,但没有受到任何伤害。

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

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

And the answer:

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

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

秉烛思 2024-12-06 21:43:27

以下补丁解决了我的问题;不再对我发出警告。

204_304_keep_alive.patch

只需编辑第 205 行的文件 httpresponse.rb 即可,如图所示在上面的链接;事实上,该链接显示了对 Ruby 未来版本所做的更正。

我在通过 RVM 作为单个用户安装的 ruby​​ 1.9.3-p0 上使用 Rails 3.2.0。所以我的例子中的位置是:

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

要更改的文件的位置根据安装类型、RVM 与否、甚至是多用户或单用户而有所不同,所以我只给出最后一部分:

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

我希望这对某人有帮助。

编辑:这是链接到更改主干分支中相关行的提交红宝石项目。

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

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.

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:

~/.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:

.../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.

九命猫 2024-12-06 21:43:27

只需将 Gem 显式添加到 Gemfile 中即可消除警告消息:

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

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

group :development do
  gem 'webrick', '~> 1.3.1'
end
毁我热情 2024-12-06 21:43:27

您还可以使用 Thin 代替默认的 Webrick。
将其添加到 Gemfile
<代码>
宝石“薄”

那么 rails s Thin 将使用 Thin,并且警告将会消失。

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

gem 'thin'

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

婴鹅 2024-12-06 21:43:27

如果您正在使用 .rvm,请执行此操作来修复它...

正如 João Soares 所提到的,所有功劳都归功于他,如果您愿意,这就是您可以做的不要消除这个关于发展的警告。

  1. 使用您最喜欢的编辑器打开此文件:

    ~/.rvm/rubies//lib/ruby/1.9.1/webrick/httpresponse.rb
    
  2. 转到包含此文件的行(对我来说实际上是第 206 行):

    是否分块? || @header['内容长度']
    
  3. 更改它,取自 此补丁,对此:

    是否分块? || @header['内容长度'] || @状态== 304 || @状态== 204
    
  4. 保存文件并最终重新启动您的 Rails 服务器

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

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.

  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):

    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

ヅ她的身影、若隐若现 2024-12-06 21:43:27

此问题已在 Ruby 的主干分支中通过提交到 webrick 得到修复。

您可以在您的设置中类似地编辑这个特定的 webrick 文件。可以通过以下方式找到大致位置:

gem which webrick

要实际编辑文件:(

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

或者使用您最喜欢的编辑器代替 nano。)

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

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

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.)

栀梦 2024-12-06 21:43:27

JRuby 版本:如果您使用 .rvm,请执行此操作来修复它...

正如 João SoaresKjellski 所提到的,这是如果您想在开发中消除此警告并且正在使用 JRuby,您可以做什么。

  1. 使用您最喜欢的编辑器打开此文件:

    ~/.rvm/rubies/jruby-<版本>/lib/ruby/<1.8 或 1.9>/webrick/httpresponse.rb
    
  2. 转到包含此文件的行(对我来说是第 205 行):

    是否分块? || @header['内容长度']
    
  3. 更改它,取自 此补丁,为此:

    是否分块? || @header['内容长度'] || @状态== 304 || @状态== 204
    
  4. < p>保存文件并最终重新启动 Rails 服务器。

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

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.

  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):

    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.

南城追梦 2024-12-06 21:43:27

另一种解决方法是从 webrick 中删除有问题的行。它只是没那么有用:(

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

您可能需要sudo

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

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

(you may need to sudo)

Spring初心 2024-12-06 21:43:27

添加

config.middleware.use Rack::ContentLength

到您的 application.rb 文件中,即使使用 webrick,警告也会消失。当渲染 json 或文本响应时,这也将在生产中正确设置 Content-Length

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文