2018年以降の記事はGitHub Pagesに移行しました

Ruby1.9.3のWebrickで出るCould not determine content-length...エラーを消す方法(2.0.0では解決済)

あらすじ

Redmineを起動している時に以下のようなログが大量に吐かれコンソールが埋め尽くされて困った。

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

環境

参考サイト

解決方法

以下のパッチをあてる 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.