Remove WEBrick console warnings in Ruby 1.9.3

Since Rails uses the WEBrick server out of the box, most of you will use it for developing and debugging Rails applications. I also prefer WEBrick, when starting to develop an application, because no additional Gems have to be installed.

But when I upgraded to Rails 3.2.2, strange messages started to appear in the WEBrick console:

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

Nothing really came across to the functionality of my app and everything rendered fine within the browser. Yet these messages started to confuse me the first time I saw them. Also if you want to examine the requests being made during development, it can be very disturbing to see all these warnings every time you look into the console. And to be honest, it’s just annoying to do debug in the console, when you have all these warnings scattered across the output.

The good part of this story is, that you can easily fix these warnings. This patch by Aaron Patterson fixed the mentioned issue by just changing one line in a core file of the WEBrick Gem. So first of all you just have to go to your Gems and search for WEBrick and the according file. Since I am using Ubuntu together with Ruby 1.9.3-p125 (installed with RVM), the location of WEBrick on my machine is:

~/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/

Depending on your OS, the usage of RVM or your Ruby version, the location can be different from mine. But the location will always be somewhere at .../ruby-yourVersionOfRuby/lib/ruby/1.9.1/webrick/

Now the first thing to do is to stop your Rails server, if it is currently running. Then navigate to your local WEBrick folder and open a file called httpresponse.rb. Afterwards, simply change line 205 to the correct line 206 as suggested by the patch:

httpresponse.rb:

# Keep-Alive connection.
if @header['connection'] == "close"
  @keep_alive = false
elsif keep_alive?
  # if chunked? || @header['content-length']
  if chunked? || @header['content-length'] || @status == 304 || @status == 204
    @header['connection'] = "Keep-Alive"
  else
    msg = "Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true"
    @logger.warn(msg)
    @header['connection'] = "close"
    @keep_alive = false
  end
else
  @header['connection'] = "close"
end

The last step is to simply restart your Rails server and all the warnings magically disappeared! Of course you can develop your application together with these warnings and simply ignore them. As this is just an minor issue by WEBrick and not of Rails, also switching to an other server like thin can get rid of these warnings. But I prefer WEBrick for local development and therefore like to have a clean console, so that I can focus on important warnings or errors.

I hope, this post was useful for all those of you, who like WEBrick as much as I do. Thanks to the patch and this question at Stackoverflow for helping me with resolving my issue. Happy coding!

About the Author

Studying MultiMedia Technology @ University of Applied Sciences in Salzburg.