Bug #14451
closedHTTP responses with Content-Length: 0 cause a failure
Description
When a web server responds with a 200 OK without a body, if read_timeout is not set to nil, a Net::Timeout exception is thrown after 60 seconds. This is because ruby is stuck looping in rbuf_fill (https://github.com/ruby/ruby/blob/814daf855e0aa2c3a1164dc765378d3a092a1825/lib/net/protocol.rb) trying to read a response that will never come (or more precisely has already come in the header; there is no body to be read).
According to the RFC for JSON, for example (RFC7231 https://tools.ietf.org/html/rfc7231#section-6.3.1) a 0 content-length is a perfectly valid JSON response:
Aside from responses to CONNECT, a 200 response always has a payload,
though an origin server MAY generate a payload body of zero length.
I encountered this issue via the rest-client library and it appears that read_timeout is passed upstream to Net::HTTP. It can be reproduced with the following script:
require 'rest-client'
result = RestClient::Request.execute(method: :post, url: 'http:/mywebserver.com/', payload: {testkey: 'testvalue'}, headers: {accept: :json}, timeout: nil)
puts result.body
You would then need to configure the webserver to return a response with no body and a content-length of zero. I would suggest that Net:HTTP should check the response header for a content-length of zero and skip attempts to read the body in this event.