开发者

What does "WARN Could not determine content-length of response body." mean and how to I get rid of it?

开发者 https://www.devze.com 2023-03-28 17:44 出处:网络
Since upgrading to Rails 3.1 I\'m seeing this warning message in my development log: WARNCould not determine content-length of response body. Set content-length of the response or set Response#chunk

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?


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.


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.


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


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.


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


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


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.


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号