あらすじ
Redmineを起動している時に以下のようなログが大量に吐かれコンソールが埋め尽くされて困った。
[yyyy-mm-dd ...] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
環境
- Ruby 1.9.3
- Redmine 2.3.1
- Rails 3.2.13
- WEBrick 1.3.1
参考サイト
- Railsのログから”Could not determine content-length …“とかでてるのを消す - Qiita [キータ]
- Webrickが出す大量のログを抑止するには? - QA@IT
- Ruby - Railsサーバーの WARN Could not determine content-length… のログを表示しないようにする - Qiita [キータ]
- rails 3.1.1.rc1 emits warning for each assets when using ruby-1.9.3-rc1 · Issue #3164 · rails/rails · GitHub
- Bug #5737: WEBrick doesn’t support keep alive connections for 204 and 304 responses - ruby-trunk - Ruby Issue Tracking System
解決方法
以下のパッチをあてる or 該当ファイルを変更する。
このパッチは取り込まれていて、最新版だとなおってるのかな?
→ 2.0.0 ではなおってた!
# Keep-Alive connection.
if @header['connection'] == "close"
@keep_alive = false
elsif keep_alive?
if chunked? || @header['content-length'] || @status == 304 || @status == 204 || HTTPStatus.info?(@status)
@header['connection'] = "Keep-Alive"
else
ちなみに、 Railsのissue を見ると、みんな発生してた様子。
RFCにはこう書いてある模様。
So if you want to do keep-alive, even if you add a content length, you will always get a warning. RFC2616 Section 4.4 says:
1.Any response message which “MUST NOT” include a message-body (such as the 1xx, 204, and 304 responses and any response to a HEAD request) is always terminated by the first empty line after the header fields, regardless of the entity-header fields present in the message
I think this means that clients will know the length of the body, and clients can support keep-alive connections with no content-length for these types of responses.